This integer constant:
9223372036854775808L
is too large to be stored in a long.
Instead, use:
9223372036854775808UL
This specifies that the constant has the type unsigned long by appending the suffix UL
Or just use the suffix U:
unsigned long max_ul = 9223372036854775808U;
When an integer constant has the suffix L (or l) then the compiler determines its type in the following order: the first of the types
signed long
signed long long
in which its value can be represented. It seems that the type signed long has the same integer representation as the type signed long long established by the compiler. So neither the type signed long nor the type signed long long can represent the constant. The constant is too big for these types. But the type unsigned long that has the same internal representation as the type unsigned long long established by the compiler can represent the constant.
Pay also attention to that there are no negative integer constants in C.
If for example you will write
int x = -1;
then the compiler splits the construction -1 into two tokens: the integer constant 1 and the unary operator -.
Consider the following demonstrative program
#include <stdio.h>
int main(void)
{
int a[] = { 0, 1, 2 };
int *p = a + 1;
printf( "p[-1] = %d\n", p[-1] );
printf( "-1[p] = %d\n", -1[p] );
return 0;
}
The program output is
p[-1] = 0
-1[p] = -2
The expression -1[p] is not the same as the expression (-1)[p]. It is processed as -(1[p] ) that is equivalent to -p[1].
longand no larger extended integer types, this code behaves differently in C90 than in later editions. In C90 the constant would have typeunsigned longeven though the suffix isL, whereas since C99 the decimal constant withoutucan only ever have a signed type (with it being a constraint violation if no suitable type exists). So it is relevant in which mode you are invoking the compiler.