c++ - 当我在 C++ 中使用 auto bi = 123456789 时,它是否总是分配为 int?

标签 c++ gcc

如果我想让 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 表,我们会发现即使是 UL 后缀的效果也取决于可以使用的大小被安置:

enter image description here

关于c++ - 当我在 C++ 中使用 auto bi = 123456789 时,它是否总是分配为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319789/

相关文章:

c - 在不修改内核的情况下拦截系统调用的最小开销方式

我可以让 gcc 在链接共享库时忽略静态库吗?

c++ - <Int, int*> 在 C++ 中的映射

C++一次运行2个进程

c++ - error C2248,这是什么错误,我该如何解决?

java - Tizen Mobile 和 iPad Linux - 它是否允许 GCC 和 Java?

c++ - 未给出显式整数后缀时,参数 'typing' 的规则是什么?

c++ - 减少到二分匹配

c++ - Qt 应用程序 UI 元素在 Docker 中随机呈现为空白/黑色

c - 为什么GCC在编译C代码时不使用更多寄存器