jorge saraiva jorge saraiva - 1 month ago 5
C++ Question

When i use auto bi = 123456789, in C++, is it always assigned as an int?

If i want bi to be an

long int
, isn't possible to use auto because it always assign as int?

Answer

Some options:

auto bi = "123456789";          // const char*
auto bi2 = 12345;               // int
auto bi3 = 123456789;           // int (when int is 32 bits or more )
auto bi4a = 123456789L;         // long
auto bi4b = 178923456789L;      // long long! (L suffix asked for long, but got long long so that the number can fit)
auto bi5a = 123456789LL;        // long long
auto bi5b = 123456784732899;    // long long (on my system it is long long, but might be different on ILP64; there is would just be an int)
auto bi6 = 123456789UL;         // unsigned long
auto bi7 = 123456789ULL;        // unsigned long long

All of the examples above depend on the system that you use.

In the standard, in [lex.icon] Table 5 — Types of integer literals is referenced:

The type of an integer literal is the first of the corresponding list in Table 5 in which its value can be represented.

If we look at the table for decimal literals we see that even the effects of the U and L suffixes depend upon what size can be accommodated:

enter image description here

Comments