加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

这两行代码中显式和隐式类型转换有什么区别?

发布时间:2020-12-16 10:21:30 所属栏目:百科 来源:网络整理
导读:char a;char b;char c;a = b + c;a = (char)((int)b+(int)c); 在第一行中,它涉及从char到int的转换.在第二行,它是明确的.编译器生成的二进制文件有区别吗? 请从嵌入式系统的角度考虑这个问题. 解决方法 我对该标准的阅读表明,这取决于实现和优化器. 第5.1.2
char a;
char b;
char c;

a = b + c;

a = (char)((int)b+(int)c);

在第一行中,它涉及从char到int的转换.在第二行,它是明确的.编译器生成的二进制文件有区别吗?

请从嵌入式系统的角度考虑这个问题.

解决方法

我对该标准的阅读表明,这取决于实现和优化器.

第5.1.2.3节,第10部分:

EXAMPLE 2 In executing the fragment

char c1,c2;
/* ... */
c1 = c1 + c2;

the ‘‘integer promotions’’ require that the abstract machine promote the value of each variable to int size and then add the two ints and truncate the sum. Provided the addition of two chars can be done without overflow,or with overflow wrapping silently to produce the correct result,the actual execution need only produce the same result,possibly omitting the promotions.

我的理解是标准允许编译器决定是否可以使用8位加法,只要结果不能与添加整数和转换为字符区分开来.

回到我在嵌入式世界(九十年代中期)的日子里,我们用于8位平台的C编译器,用于68HC11的Whitesmith编译器,产生了一个用于添加两个字符的“普通”8位加法指令.找出你的fujitsu系统中发生的事情的唯一方法是编译到汇编并自己检查.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读