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

If I want bi to be long int, is it impossible to use auto, because it is always assigned as int?

+2
source share
1 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 above examples depend on the system you are using.

In the standard, in [lex.icon]Table 5 - References to types of integer literals:

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

, , U L , :

enter image description here

+11

Source: https://habr.com/ru/post/1684828/


All Articles