简体   繁体   English

评估 `int x = -2147483648` 时会发生什么?

[英]What happens when evaluating `int x = -2147483648`?

What happens when evaluating评估时会发生什么

int x = -2147483648

? ?

  1. 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.在计算-21474836482147483648是一个 long 类型不是 int 的整数常量,因此计算 -2147483648 的结果-2147483648是 long 类型,而不是 int 类型。

  2. 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具有longlong 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 (因为两个操作数都被提升为longlong 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 .在下表中,没有后缀的十进制常量条目包含列表intlong intlong 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.

相关问题 将字符串分配给int时会发生什么? - What happens when I assign a string to an int? 将int转换为float时在后台会发生什么 - what happens at background when convert int to float 当我在C中将long int分配给int时会发生什么? - What happens when I assign long int to int in C? 当 %x 读取有符号字符时会发生什么? - what happens when %x read signed char? 当我们写 int x 时会发生什么; 并从 C 语言的主 function 返回操作系统是否为它分配 memory? - What happens when we write int x; and return from the main function in C language does OS allocate memory for it? 为什么-2147483648在适合int时会自动提升为long? - Why is -2147483648 automatically promoted to long when it can fit in int? 当我们将一个 int 分配给一个由 2 个 int 组成的数组时会发生什么? - What happens when we assign one int to an array of 2 ints? 在C中,当我们将int转换为struct *时,内存中会发生什么? - In C, what happens in memory when we do cast int to struct*? 在 C 中将 int 列表转换为 char 列表时会发生什么? - What happens when a int list is cast as a char list in C? 当我们将char *转换为int *时,在后台或内存中会发生什么 - What happens in background or in memory when we cast char * to int *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM