C 语言中的整型提升

昨天同事问了我个 C 语言的问题,没能一下答对。问题可以精简如下:

unsigned char a, b;
a = 0x0f;
b = (~a) >> 4;
printf("0x%x", b);

输出的结果为多少。

当时认为是 0x0f,可实测的结果是 0xff。查了下 C 语言的标准,确实是我错了。


K&R  C中关于整型提升(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 may be 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."


简单的说就是 char 型、short 型在做算术运算时会被提升为整型或无符号整型。因此 (~a) 被提升为 int 型的取反运算。
对于 MinGW gcc,int 型是 32 位, 因此 (~a) = 0xff ff ff f0, 所以 b = 0xff。

说实话,C 标准在这个问题上有点让人不舒服,不过之所以这样规定也是个历史遗留问题了。虽然不完美,但是我们也只能接受。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值