C 整型提高

摘自维基百科express

整型提高是C程序设计语言中的一项规定:在表达式计算时,各类整形首先要提高为int类型,若是int类型不足以表示则要提高为unsigned int类型;而后执行表达式的运算。[1]设计

这一规则是由C语言的发明人丹尼斯·里奇与肯·汤普逊创设的:[2]ip

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.get

这段话的大意是:表达式中能够使用整数的地方,就能够使用枚举类型,或有符号或无符号的字符、短整数、整数位域。若是一个int能够表示上述类型,则该值被转化为int类型的值;不然,该值被转化为unsigned int类型的值。这一过程被称做integral promotion。it

整型提高的意义在于:表达式的整型运算要在CPU的相应运算器件内执行,CPU内整型运算器(ALU)的操做数的字节长度通常就是int的字节长度,同时也是CPU的通用寄存器的长度。所以,即便两个char类型的相加,在CPU执行时实际上也要先转换为CPU内整型操做数的标准长度。通用CPU(general-purpose CPU)是难以直接实现两个8比特字节直接相加运算(虽然机器指令中可能有这种字节相加指令)。因此,表达式中各类长度可能小于int长度的整型值,都必须先转换为int或unsigned int,而后才能送入CPU去执行运算。io

相关文章
相关标签/搜索