Reputation: 2478
I am using Linux x86_64 with gcc 5.2.1 and I am using a code to ascertain the maximum value for "unsigned long long" variable in C programming language.
In my machine, the size for "unsigned long long" is 8 bytes. Doing the math tells me that the range should be 0 to (2 ** 64) - 1 which is: 0 to 18446744073709551615. The code is as follows:
#include<stdio.h>
int main()
{
unsigned long long a = 18446744073709551615;
printf("a: %llu\n", a);
return 0;
}
When I compile it, I get the following output:
Ascertaining_Range.c: In function ‘main’: Ascertaining_Range.c:5:25: warning: integer constant is so large that it is unsigned unsigned long long a = 18446744073709551615;
My question here is that why is it throwing a warning. The value is within the periphery of the range. And also, it has been declared unsigned explicitly.
Upvotes: 0
Views: 540
Reputation: 141574
Your code is correct and this is a bogus warning.
To avoid the warning you could write:
unsigned long long a = 18446744073709551615ull;
The warning is (slightly) useful for some integer literals, but not this particular one, and the compiler is not trying very hard to limit the warning to cases where it is useful.
NB. Make sure you're using -std=c99
or -gnu99
or later; before 1999 C did not officially have unsigned long long
, and different compilers did weird things with large integer literals.
Upvotes: 1