If i want bi to be an long int
, isn't possible to use auto because it always assign as int?
Asked
Active
Viewed 590 times
2

jorge saraiva
- 235
- 4
- 15
-
1Why do you think it "assigns as int"? – juanchopanza Oct 29 '16 at 13:57
-
4If you wrap it in quotes, it definitely won't be an int. It will be a `char const*` (I think). --- You can check the types of your variables like mentioned [here](http://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c) – byxor Oct 29 '16 at 13:57
-
2If you specifically want it to be a `long`, why not just say so to the compiler `long bi = `? – Bo Persson Oct 29 '16 at 13:58
-
2A string is a string. C++ have no automatic conversion between numbers in strings and actual numbers. – Some programmer dude Oct 29 '16 at 14:01
-
The keywords you're looking for are "integer literals". There are a bunch of suffixes you can use to control type. – Robinson Oct 29 '16 at 14:05
-
Your question is C++, and probably C++11, so this is just a remark concerning C. `auto` in C denotes a storage class -- on the stack. Omitting a variable's type in C89 defaulted the type to int, so that `auto i;` declared an `int` variable with automatic storage duration. If a compiler lets you get away with assigning a pointer to an int (like my gcc 5.4.0 does), then `auto i = "123456789";` declares (and defines, and initializes with an address) *an `int` variable.* – Peter - Reinstate Monica Oct 29 '16 at 14:34
-
@BrandonIbbotson You re right! I just edited now, thanks! – jorge saraiva Oct 29 '16 at 17:19
1 Answers
11
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:

wally
- 10,717
- 5
- 39
- 72
-
Fantastic answer. You could go a step further and add an example of `long long`. – byxor Oct 29 '16 at 14:03
-
1
-
I'm trying to find an SO question where the table is mentioned from the standard that describes how the type is selected for the literal. – wally Oct 29 '16 at 14:07
-
@flatmouse: You have to go down to the C standard to find range values. – Cheers and hth. - Alf Oct 29 '16 at 14:08
-
3Note that `bi5b` would be just `int` (i.e. `long long` would be incorrect) on an ILP64 architecture; see (http://www.unix.org/whitepapers/64bit.html). – Cheers and hth. - Alf Oct 29 '16 at 14:16