简体   繁体   English

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

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

I have a query about data type promotion rules in C language standard. 我有一个有关C语言标准中的数据类型升级规则的查询。 The C99 says that: 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;否则,它将转换为一个无符号int。”

My questions is in case of a C language expression where unsigned int and signed int are present, which type will be promoted to what type? 我的问题是在存在unsigned intsigned int的C语言表达式的情况下,哪种类型将提升为哪种类型?

Eg int cannot represent all the values of the unsigned int (values larger than MAX_INT values) whereas unsigned int cannot represent the -ve values, so what type is promoted to what in such cases? 例如, int不能表示unsigned int所有值(大于MAX_INT值的值),而unsigned int不能表示-ve值,那么在这种情况下什么类型被提升为什么类型?

I think you are confusing two things. 我认为您在混淆两件事。 Promotion is the process by which values of integer type "smaller" that int/unsigned int are converted either to int or unsigned int. 提升是将int / unsigned int的“较小”整数类型的值转换为int或unsigned int的过程。 The rules are expressed somewhat strangely (mostly for the benefit of handling adequately char) but ensure that value and sign are conserved. 这些规则有些奇怪(主要是为了充分处理char),但要确保其值和符号得到保留。

Then there is the different concept of usual arithmetic conversion by 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之间略有变化)

I think the following answers your question: 我认为以下回答了您的问题:

6.3.1.3 Signed and unsigned integers 6.3.1.3有符号和无符号整数

1 When 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. 1将整数类型的值转换为_Bool以外的其他整数类型时,如果该值可以用新类型表示,则该值不变。

2 Otherwise, 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. 2否则,如果新类型是无符号的,则通过重复添加或减去比新类型可表示的最大值多一个值来转换值,直到该值在新类型的范围内为止。

3 Otherwise, the new type is signed and the value cannot be represented in it; 3否则,将对新类型进行签名,并且无法在其中表示值; either the result is implementation-defined or an implementation-defined signal is raised. 结果是实现定义的,还是引发实现定义的信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么时候将类型提升为unsigned int? - When does a type get promoted to an unsigned int? 类型转换:在C中将signed int转换为unsigned long - Type conversion: signed int to unsigned long in C C: int 或 unsigned int 哪种类型我用于指针增加 - C: int or unsigned int which type I used for pointer increasement 为什么我们在C中有无符号和有符号的int类型? - Why Do We have unsigned and signed int type in C? MISRA错误:字段类型应为int,unsigned int或signed int - MISRA ERROR: field type should be int, unsigned int or signed int 类型转换 - unsigned to signed int / char - Type conversion - unsigned to signed int/char 为什么在C语言中,每个带符号的int类型都必须有一个对应的无符号的int类型? - Why in C language for every signed int type must there be a corresponding unsigned int type? 在什么情况下值会被提升为无符号整数? - In what circumstance will a value be promoted to an unsigned int? 在有符号的int和无符号的int之间进行转换时是否需要强制类型转换? - Is a type cast necessary while converting between signed int and unsigned int? 为什么 (-i) 的类型,其中 i 是 unsigned int,仍然是 unsigned int? - Why is the type of (-i), where i is unsigned int, still unsigned int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM