C语言 在存在 unsigned int 和 signed int 的 C 表达式中,哪种类型将提升为哪种类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2280663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:31:34  来源:igfitidea点击:

In a C expression where unsigned int and signed int are present, which type will be promoted to what type?

cinteger-promotion

提问by goldenmean

I have a query about data type promotion rules in C language standard. The C99 says that:

我有一个关于C语言标准中数据类型提升规则的查询。C99 说:

C integer promotions also require that "if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int."

C 整数提升还要求“如果一个 int 可以表示原始类型的所有值,则将该值转换为 int;否则,将其转换为 unsigned int”。

My questions is in case of a C language expression where unsigned intand signed intare present, which type will be promoted to what type?

我的问题是在 C 语言表达式 whereunsigned intsigned intare present 的情况下,哪种类型将提升为哪种类型?

E.g. intcannot represent all the values of the unsigned int(values larger than MAX_INTvalues) whereas unsigned intcannot represent the -ve values, so what type is promoted to what in such cases?

例如int,不能表示unsigned int(值大于MAX_INT值)的所有值而unsigned int不能表示 -ve 值,那么在这种情况下什么类型被提升为什么?

采纳答案by dirkgently

I think the following answers your question:

我认为以下回答了您的问题:

6.3.1.3 Signed and unsigned integers

1When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

3Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

6.3.1.3 有符号和无符号整数

1当一个整数类型的值转换为_Bool以外的另一个整数类型时,如果该值可以用新类型表示,则不变。

2否则,如果新类型是无符号的,则通过重复加或减一个新类型可以表示的最大值来转换该值,直到该值在新类型的范围内。

3否则,新类型是有符号的,值不能在其中表示;要么结果是实现定义的,要么引发实现定义的信号。

回答by AProgrammer

I think you are confusing two things. Promotionis the process by which values of integer type "smaller" that int/unsigned int are converted either to int or unsigned int. The rules are expressed somewhat strangely (mostly for the benefit of handling adequately char) but ensure that value and sign are conserved.

我认为你混淆了两件事。提升是将 int/unsigned int 的“较小”整数类型值转换为 int 或 unsigned int 的过程。规则表达得有些奇怪(主要是为了充分处理字符),但确保值和符号是守恒的。

Then there is the different concept of usual arithmetic conversionby which operands of arithmetic operators are converted to a common type. It begins by promoting the operand (to either int or unsigned) if they are of a type smaller than int and then choosing a target type by the following process (for integer types, 6.3.1.8/1)

然后是普通算术转换的不同概念,通过该概念将算术运算符的操作数转换为公共类型。如果操作数的类型小于 int,则首先提升操作数(为 int 或 unsigned),然后通过以下过程选择目标类型(对于整数类型,6.3.1.8/1)

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.

如果两个操作数的类型相同,则不需要进一步转换。

否则,如果两个操作数都具有有符号整数类型或都具有无符号整数类型,则将具有较小整数转换等级的类型的操作数转换为具有较大等级的操作数的类型。

否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则将具有有符号整数类型的操作数转换为具有无符号整数类型的操作数的类型。

否则,如果有符号整数类型操作数的类型可以表示无符号整数类型操作数类型的所有值,则将无符号整数类型操作数转换为有符号整数类型操作数的类型。

否则,两个操作数都被转换为与带符号整数类型的操作数的类型对应的无符号整数类型。

(Note that ISTR that those rules have changed slightly between C89 and C99)

(请注意 ISTR,这些规则在 C89 和 C99 之间略有变化)