C 中的隐式整数类型转换

Implicit integer type conversion in C

我了解 C 语言在整数和浮点类型之间的隐式转换,但我对 signed/unsigned 隐式类型转换有疑问。

例如,如果添加 unsigned charsigned int,结果类型是什么?是 unsigned intsigned int 还是其他?

我在 C99 ANSI 标准中没有看到任何关于此的具体内容,因此非常感谢您的帮助。

几乎肯定会是 signed int,这取决于运行代码的系统。检查段落积分促销here

unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise.

例如,

在 POSIX 系统上肯定是 signed int,因为 char 总是 8 位,而 int 至少是 16 位。因此 int 可以表示 unsigned char 的所有可能值。显然 systems 其中 char 多于 8 位。

在C99中,引用是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:

  • 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 is long 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 is double.
  • 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 is float. 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 charsigned int 时,要么:

  • 首先将 unsigned char 提升为 int,然后两种类型相同,因此结果的类型为 int,或
  • (不常见)int 不能表示所有可能的 unsigned char 值。在这种情况下,unsigned char 被提升为 unsigned int,并且第三个子项目符号适用:unsigned intint 具有相同的等级,因此 int 操作数是转换为 unsigned int,结果类型为 unsigned int.