如果我想让 bi 成为一个 long int
,难道不能使用 auto 因为它总是赋值为 int 吗?
最佳答案
一些选项:
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
以上所有示例都取决于您使用的系统。
在标准中,[lex.icon]
表 5 — 整数文字类型被引用:
The type of an integer literal is the first of the corresponding list in Table 5 in which its value can be represented.
如果我们查看decimal literals 表,我们会发现即使是 U
和 L
后缀的效果也取决于可以使用的大小被安置:
关于c++ - 当我在 C++ 中使用 auto bi = 123456789 时,它是否总是分配为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319789/