整形提升:
K&R(C语言第一本编程书的编著者)在C中将整形提升定义为:
“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.”
大致意思分为两点:
- 1.只要一个表达式中用到了整形,那么类型为char, short int或整形位域的变量,以及枚举的类型都可以被放在这个整形变量的位置。
- 2.如果条件一中的原始类型值域可以被int表示,那么原值转换为int,否则的话,转换unsigned。
以上两点的结合,被称为整形提升
举例说明:
- 1.关于条件1的举证:
#include<iostream>
using namespace std;
int main()
{
unsigned char a = 100;
unsigned char b = 200;
unsigned