"A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer maybe used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion."
“In any expression, you can always use a value whose type ranks lower than
int
in place of an operand of type
int
or unsigned
int
. You can also use a bit-field as an integer operand,In these cases, the compiler applies integer promotion : any operand whose type ranks lower than
int
is automatically converted to the type
int
, provided
int
is capable of representing all values of the operand's original type。”
这段话摘自C语言核心技术英文版。大意也就是说,在一些语句中如果使用了级别比int或者unsigned int低的整型类型,甚至位字段、枚举类型时,编译器将应用整型提升规则:所有比int级别低的整型数据将自动被转换为int型。
注意事项:
1. 此过程发生在运算过程中,以前是为了照顾cpu寄存器的处理,现在是保证老程序兼容性的强制标准
2. 低于int或unsigned int 表示范围的整型类型,会被编译器自动整型提升,不管表达式中的数据类型是一致的还是需要互相转换的。举例来说:
char a , b ,c;
c=a + b;
在上述过程中,尽管两个运算符"+"和"="的操作数全为char型,但在中间计算过程中存在着整数提升:对于表达式a+b ,a、b都是char型,因此被提升至int型后,执行“+”运算,计算结果(int型)再赋值给c(char型),又执行了隐式的类型转换。