C中的隐式整数类型转换 [英] Implicit integer type conversion in C
问题描述
我了解整数和浮点类型之间C语言的隐式转换,但是我对有符号/无符号隐式类型转换有疑问.
I understand the implicit conversions of the C language between integer and floating point types, but I have a question for signed/unsigned implicit type conversions.
例如,如果添加unsigned char
和signed int
,结果类型将是什么?是unsigned int
,signed int
还是其他?
If you add, for example, an unsigned char
and a signed int
, what will be the resulting type? Would it be an unsigned int
, a signed int
, or something else?
我没有看到C99 ANSI标准中与此相关的任何内容,因此可以提供任何帮助.
I don't see anything specific in the C99 ANSI standard about this, so any help is appreciated.
推荐答案
在C99中,引用为6.3.1.8常规算术转换".
In C99, the reference is 6.3.1.8 "Usual arithmetic conversions".
许多期望算术类型的操作数的运算符会导致转换并产生结果 以类似的方式输入.目的是为操作数确定普通实型 结果.对于指定的操作数,将转换每个操作数,而无需更改类型 域,将其类型转换为对应的实型是普通实型.除非 除非另有明确说明,否则普通实型也是对应的实型 结果,如果操作数相同,则其类型域是操作数的类型域, 否则就很复杂.这种模式称为常规算术转换:
Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:
- 首先,如果两个操作数的对应实型为
long double
,则另一个 在不更改类型域的情况下,将操作数转换为其类型 对应的实型是long double
. - 否则,如果两个操作数的对应实型为
double
,则另一个 在不更改类型域的情况下,将操作数转换为其类型 对应的实型是double
. - 否则,如果两个操作数的对应实型为
float
,则另一个 在不更改类型域的情况下,将操作数转换为其类型 对应的实型为float
. 51) - 否则,将对两个操作数执行整数提升.然后
以下规则适用于提升后的操作数:
- 如果两个操作数具有相同的类型,则无需进一步转换.
- 否则,如果两个操作数都具有符号整数类型或都具有无符号类型 整数类型,具有较小整数转换等级的类型的操作数为 转换为具有更高等级的操作数的类型.
- 否则,如果具有无符号整数类型的操作数的秩更大或 等于另一个操作数类型的等级,然后是 有符号整数类型将转换为无符号操作数的类型 整数类型.
- 否则,如果带符号整数类型的操作数的类型可以表示 具有无符号整数类型的操作数类型的所有值,然后 无符号整数类型的操作数将转换为 带符号整数类型的操作数.
- 否则,两个操作数都将转换为无符号整数类型 对应于带符号整数类型的操作数的类型.
- First, if the corresponding real type of either operand is
long double
, the other operand is converted, without change of type domain, to a type whose corresponding real type islong double
. - Otherwise, if the corresponding real type of either operand is
double
, the other operand is converted, without change of type domain, to a type whose corresponding real type isdouble
. - Otherwise, if the corresponding real type of either operand is
float
, the other operand is converted, without change of type domain, to a type whose corresponding real type isfloat
. 51) - Otherwise, the integer promotions are performed on both operands. Then the
following rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
- Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
加法执行通常的算术转换,因此,在添加
unsigned char
和signed int
时,可以:Addition performs the usual arithmetic conversions, so, when adding
unsigned char
andsigned int
, either:- 首先将
unsigned char
提升为int
,然后两种类型都相同,因此结果的类型为int
,或者 - (不常见)
int
不能代表所有可能的unsigned char
值.在这种情况下,unsigned char
被提升为unsigned int
,并且应用第三个子项目:unsigned int
具有与int
相同的等级,因此int
操作数被转换为unsigned int
,结果为键入unsigned int
.
- first the
unsigned char
is promoted toint
, and then both types are the same, so the result has typeint
, or - (uncommon)
int
cannot represent all possibleunsigned char
values. In this case,unsigned char
is promoted tounsigned int
, and the third sub-bullet applies:unsigned int
has equal rank toint
, so theint
operand is converted tounsigned int
, and the result has typeunsigned int
.
这篇关于C中的隐式整数类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!