为什么GCC在将无符号长整型设置为2 ^ 64-1时发出警告? [英] Why does GCC give a warning when setting an unsigned long to 2^64-1?

查看:186
本文介绍了为什么GCC在将无符号长整型设置为2 ^ 64-1时发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准规定一个 long int 至少是4个字节 - 在我的系统上它是8个字节。



这意味着我可以在 long 和2 64 中存储高达2 ^ 63 1 unsigned long



但是,当下面的代码用 -Wall 标志给出警告 [Wimplicitly-unsigned-literal]

 int main(int argc,char ** argv){

unsigned long a;
a = 18446744073709551615; // 2 ^ 64-1

}

如果我使用2 <如果没有警告(如预期的那样 - 2 63 -1将适合 signed long int -1(9223372036854775807) c $ c $)。

对于一个项目,我需要在 unsigned long 中具有最大值,并且我发现(9223372036854775807<< 1)+ 1 不会引发此警告。然后,我的老师建议我可以使用在 limits.h 中定义的 ULONG_MAX ,这不会给出任何警告。



为什么我不能在没有警告的情况下做这个事情,说它隐式转换 - 当我明确声明时?

解决方案根据C标准,没有后缀的十进制常量的类型是 int long int long long int ,特别是那些足以表示值的第一个值。在你的C实现中,这些都不能代表18446744073709551615,因为它太大了。

为了适应你,编译器将它的类型设置为 unsigned long 。从技术上讲,这不符合C标准,因此编译器会警告您。



在这种情况下,不会造成任何危害,因为您将值分配给 unsigned long 。但在某些情况下,使用错误的类型可能会导致问题,所以一般情况下,您应该为这些常量附加后缀,以确保它们与打算使用的方式相匹配。在这种情况下, u 就足够了;与未固定类型一样,编译器将决定是否使用 unsigned int unsigned long int unsigned long long int ,具体取决于数字的大小和类型的能力。


The C standard states that a long int is at least 4 bytes - on my system it is 8 bytes.

This means I can store values up to 2^63-1 in a long and 264-1 in an unsigned long.

However, when the following code is compiled with the -Wall flag it gives the warning [Wimplicitly-unsigned-literal]:

int main (int argc, char ** argv) {

  unsigned long a;
  a = 18446744073709551615; // 2^64-1

}

If I use 263-1 (9223372036854775807) instead, it compiles with no warnings (as expected - 263-1 will fit in a signed long int).

For a project I needed to have the maximum value in an unsigned long, and I discovered that (9223372036854775807 << 1) + 1 will not raise this warning. My teacher then suggested that I could use the ULONG_MAX defined in limits.h and this gave no warnings.

Why can't I do this without a warning saying it was converted implicitly - when I declared it explicitly?

解决方案

Per the C standard, the type of a decimal constant without a suffix is int, long int, or long long int, specifically the first of those that is sufficient to represent the value. In your C implementation, none of those can represent 18446744073709551615, because it is too large.

In order to accommodate you, the compiler is making its type unsigned long. Technically, this does not conform to the C standard, so the compiler is warning you.

In this case, no harm is caused, because you are assigning the value to an unsigned long. But there are situations in which using the wrong type can cause problems, so generally you should append a suffix to such constants to ensure they match how they are intended to be used. In this case, a u is sufficient; as with unsuffixed types, the compiler will decide whether to use unsigned int, unsigned long int, or unsigned long long int depending on the magnitude of the number and the capabilities of the types.

这篇关于为什么GCC在将无符号长整型设置为2 ^ 64-1时发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆