This answer attempts to show why the LL
is needed. (It is not easy.)
Other answers have shown why parentheses are needed.
Let's code three macros, all are decimal constants.
#define MAXILL (9223372036854775807LL)
#define MAXIL (9223372036854775807L)
#define MAXI (9223372036854775807)
Even though MAXI
has no suffix, that does not make it type int
. MAXI
will have the first type it fits in, either int
, long
, long long
or an extended integer type.
Even though MAXIL
has the L
suffix, it will have the first type it fits in, either long
, long long
or an extended integer type.
Even though MAXILL
has the LL
suffix, it will have the first type it fits in, either long long
or an extended integer type.
In every case, the macros have the same value, but potentially different types.
See C11dr §6.4.4.1 5 for type determination.
Let's try printing them and have int
and long
are 32-bit and long long
and intmax_t
are 64.
printf("I %d %d %d", MAXI, MAXIL, MAXILL); //Error: type mismatch
printf("LI %ld %ld %ld", MAXI, MAXIL, MAXILL); //Error: type mismatch
printf("LLI %lld %lld %lld", MAXI, MAXIL, MAXILL); //Ok
All three in the last line are correct as all three macros are long long
, the preceding have type mis-matches between format specifier and the number.
If we have int
is 32-bit and long
, long long
and intmax_t
are 64, then the following are correct.
printf("LI %ld %ld", MAXI, MAXIL);
printf("LLI %lld", MAXILL);
The maximum width integer type has a format specifier of "%" PRIdMAX
. This macro cannot expand to "%ld"
and "%lld"
to accommodate MAXI, MAXIL, MAXILL
. It is set to "%lld"
and numbers related to intmax_t
need to have the same type. In this case, long long
and only form MAXILL
should be used.
Other implementations could have an extended integer type (like int128_t
), in which case an implementation specific suffix or some mechanism could be used.