本文的copyleft归gfree.wind@gmail.com所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:gfree.wind@gmail.com
博客:linuxfocus.blog.chinaunix.net
Usual Arithmetic Conversion: The integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed.
Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
献丑翻译成中文:
整数提升:整数提升可以应用于所有的操作数,下面是整数提升的规则:
如果两个操作数具有相同的类型,那么不需要任何的转换;
不然,如果两个操作数同为有符号整数或者同为无符号整数,则rank小的操作数需要被提升为rank大的操作数的类型;
不然,一个无符号操作数的rank大于或者等于另外一个有符号操作数,则该有符号操作数的类型会提升为无符号操作数的类型;
不然,一个有符号操作数可以表示另一个无符号数的所有的值,那么那个无符号操作数的类型需转换为该有符号类型;
不然,两个操作数都需要转换为有符号数对应的无符号类型——这种情况,我没有找出实例,哪位朋友指点一下呢。
虽然整数类型提升为C的基础概念,但是不看标准的话,我想大多数人不会特别清楚——包括我在内,即使平时因为注意这个问题,而不会因此犯错。尤其是第4条,至少我以前就不甚了解。我会认为有符号数都会转为无符号数的,不会认为无符号数也会转为有符号数的。
更新:
阅读(7489) | 评论(12) | 转发(1) |