Home >Backend Development >C++ >When is an Integer Literal Not an `int`?

When is an Integer Literal Not an `int`?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-17 06:02:24155browse

When is an Integer Literal Not an `int`?

Type of Integer Literals Not int by Default

In programming, the type of an integer literal is typically assumed to be int. However, there are cases where this may not be the case.

Consider the following C code:

for (i = 0; i < 10000000000; i++)

Despite the literal 10000000000 not being appended with an 'L' suffix (which explicitly denotes a long integer type), the compiler recognizes that this value exceeds the range of a 32-bit integer.

According to both the C99 and C 11 standards, the type of an integer constant is determined by the first entry in Table 6 in which its value can be represented. For decimal constants without suffixes, Table 6 specifies the following types in descending order:

  • long long int
  • long int
  • int

Therefore, in the given code sample, the compiler interprets the literal as a long int (or long long int if long int is 32 bits) to accommodate its large value.

It is important to note that "too big" literals generally result in a compilation error. This is because the value cannot be represented by any of the allowed types.

The above is the detailed content of When is an Integer Literal Not an `int`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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