Daily bit(e) of C++ | integer literals
Daily bit(e) of C++ ♻️102, The complex rules for integer literal type.
Integer literals in C++ can be surprisingly complex if you care about their type. The type is determined by the combination of the specified suffix (if any), the base (e.g., decimal), and the literal's value.
For any value, the first/smallest type that can represent it is selected from a list determined by the suffix and the base.
For a decimal base, the unsigned types are only selected if the u suffix is specified. For other bases, an unsigned type can be selected even when the u suffix is not specified. In either case, the u suffix removes the possibility of a signed type being selected.
The size suffix then limits the list of potential types to at least the specified size (long, long long).
An alternative to suffixes are macros from the cstdint header that will annotate a literal to map to the corresponding std::(u)int_leastXX_t type.