关于C数据类型和常量的问题

发布于 2024-10-26 11:16:52 字数 1050 浏览 1 评论 0原文

问候! 我正在尝试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 LLL

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-11-02 11:16:52

根据 ISO C99 标准第 6.4.4.1 节(整型常量)的语义小节,整型常量的类型是下表中的第一种类型,其中值可以表示:

                                    Octal or Hexadecimal
Suffix            Decimal Constant            Constant
none         int                    int
             long int               unsigned int
             long long int          long int
                                    unsigned long int
                                    long long int
                                    unsigned long long int

u or U       unsigned int           unsigned int
             unsigned long int      unsigned long int
             unsigned long long int unsigned long long int

l or L       long int               long int
             long long int          unsigned long int
                                    long long int
                                    unsigned long long int

both u or U  unsigned long int      unsigned long int
and l or L   unsigned long long int unsigned long long int

ll or LL     long long int          long long int
                                    unsigned long long int

both u or U  unsigned long long int unsigned long long int
and ll or LL

特定的实现可以具有遵循与上面相同的模式的扩展整数类型。

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:

                                    Octal or Hexadecimal
Suffix            Decimal Constant            Constant
none         int                    int
             long int               unsigned int
             long long int          long int
                                    unsigned long int
                                    long long int
                                    unsigned long long int

u or U       unsigned int           unsigned int
             unsigned long int      unsigned long int
             unsigned long long int unsigned long long int

l or L       long int               long int
             long long int          unsigned long int
                                    long long int
                                    unsigned long long int

both u or U  unsigned long int      unsigned long int
and l or L   unsigned long long int unsigned long long int

ll or LL     long long int          long long int
                                    unsigned long long int

both u or U  unsigned long long int unsigned long long int
and ll or LL

Particular implementations can have extended integer types that follow the same pattern as above.

苍风燃霜 2024-11-02 11:16:52

也许,默认情况下,编译器假设您输入的是有符号整数。当您给它 4294967295 时,该数字不适合 4 字节整数,因此它使用 8 字节整数来存储它。然后它必须进行有损转换(long long,又称为 8 字节,到 long,又称为 4 字节),因此它会向您发出警告。

但是,当您输入 4294967295U 时,它知道您需要一个无符号整数。该数字适合 4 字节无符号整数,因此它具有 long int 类型,并且不需要有损转换。 (从 unsigned long intlong 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, to long, 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 type long int, and no lossy conversion is necessary. (You're not losing data by going from unsigned long int to long int, just mis-representing it.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文