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
■网友
是的。


    推荐阅读