Integers Without Suffixes: Promoted to Larger Types if Necessary
The original query explored why an iteration to 10 billion resulted in an unexpected behavior. One suspicion was that integer literals without suffixes might not default to the usual int type.
In both C and C , such literals are automatically promoted to a fitting integer type. This ensures that their value can be represented within the limits of at least one of the available integer types.
C and C Standards
According to the C 11 standard ([lex.icon] ¶2) and the C99 standard (§6.4.4.1), an integer literal without a suffix is given the smallest type from the following list that can accommodate its value:
In the case of 10 billion, this integer constant would be classified as long int (or long long int if long int is defined as 32-bit).
Compilation Errors for Excessively Large Literals
Both C99 and C 11 state that literals that exceed the representable range for all allowed integer types result in a compilation error. However, some compilers may handle this in non-standard ways, such as silently truncating or discarding the excess digits.
The above is the detailed content of Why Do Unsigned Integer Literals in C and C Promote to Larger Types?. For more information, please follow other related articles on the PHP Chinese website!