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

car*_*995 2 c variables integer constants

问候!我正在尝试使用C语言,直到遇到一些非常奇怪的事情.我无法向自己解释下面显示的结果.

代码:

#include <stdio.h>

int main(void)
{
    int num = 4294967295U;
    printf("%u\n", num);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题:

1.)如你所见,我创建了一个int可以保存-21474836482147483647之间的数字 .

2.)当我将值4294967295分配给此变量时,IDE在编译期间向我显示警告消息,因为变量溢出.

3.)由于好奇心,我在数字后面加了一个U(无符号),当我重新编译它时,编译器没有返回任何警告信息.

4.)我通过将U(无符号)更改为L(长)LL(长long)进行了进一步的实验.正如预期的那样,警告消息仍然存在于这两个但不是在我将其更改为UL(无符号长整数)ULL(无符号长多长)之后.

5.)为什么会这样?

警告信息:(对于步骤2)

warning #2073: Overflow in converting constant expression from 'long long int' to 'int'.
Run Code Online (Sandbox Code Playgroud)

警告信息:(对于步骤4 LLL)

warning #2073: Overflow in converting constant expression from 'long long int' to 'long int'.
Run Code Online (Sandbox Code Playgroud)

最后,感谢您阅读我的问题,非常感谢您的教导和建议.

nin*_*alj 6

具体根据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
Run Code Online (Sandbox Code Playgroud)

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

  • 同样在6.3.1.3/3中,声明"新类型已签名且值无法在其中表示;结果为**实现定义**". (2认同)