Home > Backend Development > C++ > Why Do Unsigned Integer Literals in C and C Promote to Larger Types?

Why Do Unsigned Integer Literals in C and C Promote to Larger Types?

Patricia Arquette
Release: 2024-12-12 19:30:15
Original
701 people have browsed it

Why Do Unsigned Integer Literals in C and C   Promote to Larger Types?

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:

  • int
  • long int
  • long long int

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template