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?