[英]What happens when evaluating `int x = -2147483648`?
What happens when evaluating评估时会发生什么
int x = -2147483648
? ?
When evaluating -2147483648
, 2147483648
is a integer constant which has long type not int, so the result -2147483648 of evaluating -2147483648
is of type long, not int.在计算-2147483648
, 2147483648
是一个 long 类型不是 int 的整数常量,因此计算 -2147483648 的结果-2147483648
是 long 类型,而不是 int 类型。
When evaluating the assignment "int x = ...", the RHS is value -2147483648 of long type, which is in the range of x
's type int
.在评估赋值“int x = ...”时,RHS 是 long 类型的值 -2147483648,它在x
的类型int
的范围内。 Will value -2147483648 be implicitly converted from long to int, and the conversion keep the value -2147483648 unchanged?值 -2147483648 是否会从 long 隐式转换为 int,并且转换保持值 -2147483648 不变?
Thanks.谢谢。
Your analysis in this question is right.你对这个问题的分析是对的。 Because the value is in the range of int
and is converted to int
as part of the initialization (note: the same would happen for assignment), everything works as intended.因为该值在int
范围内,并且作为初始化的一部分被转换为int
(注意:赋值也会发生同样的情况),所以一切都按预期工作。
As for the hidden part of your question you didn't ask, since it keeps getting closed as a duplicate, this does not mean you can define INT_MIN
as -2147483648
.至于您没有问的问题的隐藏部分,因为它一直作为重复关闭,这并不意味着您可以将INT_MIN
定义为-2147483648
。 The magic is in the =
(as assignment operator or a token in the initialization construct).神奇之处在于=
(作为赋值运算符或初始化构造中的标记)。 In contexts where it's not being used, there are all sorts of ways that -2147483648
having type long
or long long
rather than int
breaks semantic requirements on INT_MIN
.在不使用它的上下文中,有各种各样的方式-2147483648
具有long
或long long
类型而不是int
打破对INT_MIN
语义要求。 For example:例如:
(INT_MIN < 0U) == 0
(because both operands are promoted to unsigned
), but (INT_MIN < 0U) == 0
(因为两个操作数都被提升为unsigned
),但是(-2147483648 < 0U) == 1
(because both operands are promoted to long
or long long
). (-2147483648 < 0U) == 1
(因为两个操作数都被提升为long
或long long
)。This answer presumes the C implementation uses a 32-bit int
and a 64-bit long
.此答案假定 C 实现使用 32 位int
和 64 位long
。
C 2018 6.4.4.1 says “The type of an integer constant is the first of the corresponding list in which its value can be represented.” C 2018 6.4.4.1 说“整数常量的类型是可以表示其值的相应列表中的第一个。” In the table that follows, the entry for decimal constants with no suffix contains the list int
, long int
, long long int
.在下表中,没有后缀的十进制常量条目包含列表int
、 long int
、 long long int
。 Since a long int
is the first of those that can represent 2,147,483,648, 2147483648
has the type long int
.由于long int
是第一个可以表示 2,147,483,648 的long int
,因此2147483648
的类型为long int
。
Per 6.5.3.3 3, the result of -
is the promoted type.每6.5.3.3 3,结果-
是升级后的类型。 The integer promotions (6.3.1.1 2) have no effect on long int
.整数提升 (6.3.1.1 2) 对long int
没有影响。 So the type of -2147483648
is long int
.所以-2147483648
的类型是long int
。
Per 6.7.9 11, the value of the initializer is converted as in simple assignment.根据 6.7.9 11,初始化器的值被转换为简单赋值。 Per 6.5.16.1 2 and 6.5.16 3, the value is converted to the type of the object being assigned would have after lvalue conversion.根据 6.5.16.1 2 和 6.5.16 3,该值被转换为左值转换后被赋值的对象的类型。 That is, for an assignment to an int
object, the type is an int
value.也就是说,对于int
对象的赋值,类型是一个int
值。
Per 6.3.1.3 1, when converting a value of integer type to another integer type, if the new type can represent the value, it is unchanged.根据 6.3.1.3 1,将整数类型的值转换为另一种整数类型时,如果新类型可以表示该值,则不变。 Since int
can represent −2,147,483,648, it is unchanged.由于int
可以表示 -2,147,483,648,所以不变。
Therefore, the result of int x = -2147483648;
因此, int x = -2147483648;
is that x
is initialized with the value −2,147,483,648.是x
用值 -2,147,483,648 初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.