C语言的隐式类型转换的问题
是
■网友
先回答题主的疑问:是的,也会提升。再来吐槽:这段话的说法很怪异,比如整型其实不指int,再比如“小于整型”咋算(注意,与大小不一定有关联)?不过这种行为确实存在:C1X 6.3.1.12 The following may be used in an expression whereveran int or unsigned int may be used:— An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.— A bit-field of type _Bool, int, signed int, or unsigned int.If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int.These are called the integer promotions. All other types are unchanged by the integer promotions.而在后面usual arithmetic conversion中,在处理完浮点数的情况之后第一件进行的事情就是integer promotion。C中(直接或者包含在其它的转换之中)执行integer promotion的运算符有:调用一个没有原型的函数,或者函数原型中的省略号部分一元+,一元-,~二元*,/,%\u0026gt;,===,!=二元\u0026amp;,^,|?:总结:这段话大体上是正确的,但是:- 使用了奇怪的术语- 未指明也可能转换到unsigned(例如和int一样大的unsigned short)- 不完整((),==,!=等等显然难以称作算术表达式)
■网友
你都看到提升了,书上也说char当做int处理在一个表达式中,凡是可以用整型的地方,都可以用带符号的或者无符号的字符 短整型等。如果原始类型的所有值能用int表示,则转为int
■网友
对的,《C专家编程》8.3说到了char, bit-field, enum...都会提升为int#include \u0026lt;stdio.h\u0026gt;int main(int argc, char* argv){\tprintf("%d\", sizeof \u0026#39;A\u0026#39;);\tprintf("%d\", sizeof 1);\treturn 0;}Ubuntu16.04下输出结果为 4 4
【C语言的隐式类型转换的问题】 但是有一点比较诡异,VS下输出的结果是1 4
我猜这跟ANSI C规定的“如果编译器能够保证运算结果一致,也可以省略类型提升”有关?
■网友
对,而且应该是先提升再cast
■网友
是的。
推荐阅读
- OC为何跌出语言榜前十
- 怎样评价类似前橙会、百老汇、南极圈这样类型的离职帮抱团,对企业的积极意义和消极意义
- dart这编程语言现在发展怎么样了,语法与Java,c#很相似,甚至更简洁
- 人民车市|新语言、新起点,捷达VS7 强势入局
- C语言 指针引用数组的地址问题
- 零基础入门学习啥语言好
- 营销型外贸网站用哪种建站程序和语言比较好呢主要是适合优化,可扩展兼容性,安全性,后期网站扩展升级
- 有哪些轻易就被识别的淘宝刷单评论语言
- C语言指数函数代码
- 我现在在学c语言,然后以后的工作目标是腾讯网易这些游戏公司,我是通信专业,请问我接下来再该学些啥呢
