整型提升

C++ primer中关于整型提升(integral promotion)的定义为:

"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
=+ b;

 在上述过程中,尽管两个运算符"+"和"="的操作数全为char型,但在中间计算过程中存在着整数提升:对于表达式a+b ,a、b都是char型,因此被提升至int型后,执行“+”运算,计算结果(int型)再赋值给c(char型),又执行了隐式的类型转换。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值