[C语言] STC单片机C语言默认类型转换的问题

[复制链接]
2078|6
 楼主| zengx_xue 发表于 2013-10-18 09:41 | 显示全部楼层 |阅读模式
unsigned char  testuChar;
unsigned long  testuLong;


testuChar = 7;
testuLong = testuChar*100000;  //7000000
testuLong = testuChar*10000;   //错误值
testuLong = (unsigned long)testuChar*10000;   //70000
testuLong = testuChar*1000;  //7000
testuLong = testuChar*100; //7000  错误值
testuLong = testuChar*10;  //7000  错误值

testuLong = testuChar;    //7

还请各位高手不吝赐教!
ayb_ice 发表于 2013-10-18 10:06 | 显示全部楼层
testuLong = testuChar*10000;   //错误值

错误就对了

testuChar是8位类型,10000是16位类型,结果仍是16位类型,结果可能溢出了
coody 发表于 2013-10-18 12:39 | 显示全部楼层
这个跟STC一毛钱的关系都没有,只跟编译器有关。

testuLong = testuChar*100; //7000  错误值

KEIL会先拿两个unsigned char相乘(结果还是unsigned char,高字节丢弃),再送testuLong。
andyjian 发表于 2013-10-29 16:51 | 显示全部楼层
看的有些模糊
bingmcu 发表于 2013-10-29 17:48 | 显示全部楼层
结果可能溢出了
lhchen922 发表于 2013-10-29 18:57 | 显示全部楼层
gincn 发表于 2013-10-29 21:44 | 显示全部楼层
我试了一下,结果与楼主不一样,如下:

testuChar = 7;
testuLong = testuChar*10000;   //4464 错误值
testuLong = testuChar*(unsigned int)10000;   //4464 错误值
testuLong = testuChar*(unsigned long)10000;   //70000_对的

testuLong = testuChar*(unsigned char)100; //700_对的
testuLong = testuChar*(unsigned int)100; //700_对的
testuLong = testuChar*100; //700_对的

testuLong = testuChar*(unsigned char)10;  //70_对的
testuLong = testuChar*(unsigned int)10;  //70_对的
testuLong = testuChar*10;  //70_对的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

35

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部