关于C数据类型和常量的问题
问候! 我正在尝试C语言,直到遇到了一些非常奇怪的事情。 我无法解释自己的结果如下所示。
代码:
#include <stdio.h>
int main(void)
{
int num = 4294967295U;
printf("%u\n", num);
return 0;
}
问题:
1.) 如您所见,我创建了一个 int
,它可以容纳 -2147483648 到 2147483647。
2.) 当我将值 4294967295 赋给该变量时,IDE 在编译期间会显示一条警告消息,因为变量溢出。
3.)出于好奇,我在数字后面添加了一个U(无符号),当我重新编译它时,编译器没有返回任何警告消息。
4.) 我通过将U(无符号)更改为L(长)和LL(长长)进行了进一步的实验。正如预期的那样,这两个警告消息仍然存在,但在我将其更改为UL(无符号长整型)和ULL(无符号长整型)后就不再出现。
5.) 为什么会发生这种情况?
警告消息:(对于步骤 2)
warning #2073: Overflow in converting constant expression from 'long long int' to 'int'.
警告消息:(对于步骤 4 LL 和 L)
warning #2073: Overflow in converting constant expression from 'long long int' to 'long int'.
最后,感谢您阅读我的问题,非常感谢您的教导和建议。
Greetings!
I was experimenting with C language till I encountered something very strange.
I was not able to explain myself the result shown below.
The Code:
#include <stdio.h>
int main(void)
{
int num = 4294967295U;
printf("%u\n", num);
return 0;
}
The Question:
1.) As you see, I created an int
which can hold numbers between -2147483648 to 2147483647.
2.) When I assign the value 4294967295 to this variable, the IDE shows me a warning message during compilation because the variable overflowed.
3.) Due to curiosity I added a U (unsigned) behind the number and when I recompiled it, the compiler did not return any warning message.
4.) I did further experiments by changing the U (unsigned) to L (long) and LL (long long). As expected, the warning message still persist for these two but not after I change it to UL (unsigned Long) and ULL (unsigned long long).
5.) Why is this happening?
The Warning Message :(For steps 2)
warning #2073: Overflow in converting constant expression from 'long long int' to 'int'.
The Warning Message:(For steps 4 LL & L)
warning #2073: Overflow in converting constant expression from 'long long int' to 'long int'.
And last, thanks for reading my question, your teachings and advices are much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 ISO C99 标准第 6.4.4.1 节(整型常量)的语义小节,整型常量的类型是下表中的第一种类型,其中值可以表示:
特定的实现可以具有遵循与上面相同的模式的扩展整数类型。
As per the ISO C99 standard, section 6.4.4.1 (Integer Constants), subsection Semantics, the type of an integer constant is the first type of the following table where the value can be represented:
Particular implementations can have extended integer types that follow the same pattern as above.
也许,默认情况下,编译器假设您输入的是有符号整数。当您给它
4294967295
时,该数字不适合 4 字节整数,因此它使用 8 字节整数来存储它。然后它必须进行有损转换(long long
,又称为 8 字节,到long
,又称为 4 字节),因此它会向您发出警告。但是,当您输入
4294967295U
时,它知道您需要一个无符号整数。该数字适合 4 字节无符号整数,因此它具有 long int 类型,并且不需要有损转换。 (从unsigned long int
到long int
不会丢失数据,只是错误地表示了它。)Perhaps, by default, the compiler assumes you're typing in signed integers. When you give it
4294967295
, that number doesn't fit into a 4-byte integer, so it uses an 8-byte integer to store it, instead. Then it has to do a lossy conversion (long long
, AKA 8-byte, tolong
, AKA 4-byte), so it gives you a warning.However, when you type
4294967295U
, it knows you want an unsigned integer. That number fits into a 4-byte unsigned integer, so it has typelong int
, and no lossy conversion is necessary. (You're not losing data by going fromunsigned long int
tolong int
, just mis-representing it.)