c++ - 以零开头的数字有什么特别之处?

标签 c++ c syntax compiler-construction numbers

这是个愚蠢的问题,但对我来说很有趣)

这是我用 visual studio 2013 得到的

int i = 07;     // i == 7
int i = 16;     // i == 16
int i = 00016;  // i == 14, why?
int i = 05016;  // i == 2574, wow )
int i = 08;     // compile error, compiler expects octal number...

如果 number 以 0 开头且包含 8,则编译错误。这是正常的吗?如果 00016 == 14,编译器究竟如何处理起始零?

感谢大家))

最佳答案

是的,这是预期的。

[C++11: 2.14.2/1]: An integer literal is a sequence of digits that has no period or exponent part. An integer literal may have a prefix that specifies its base and a suffix that specifies its type. The lexically first digit of the sequence of digits is the most significant. A decimal integer literal (base ten) begins with a digit other than 0 and consists of a sequence of decimal digits. An octal integer literal (base eight) begins with the digit 0 and consists of a sequence of octal digits.22 A hexadecimal integer literal (base sixteen) begins with 0x or 0X and consists of a sequence of hexadecimal digits, which include the decimal digits and the letters a through f and A through F with decimal values ten through fifteen. [ Example: the number twelve can be written 12, 014, or 0XC. —end example ]

22 The digits 8 and 9 are not octal digits.

关于c++ - 以零开头的数字有什么特别之处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568200/

相关文章:

c++ - 无法以标准用户身份创建或写入文件

c - 在函数中设置字符数组

arrays - 如何将哈希表添加到多维数组?无法通过成员枚举分配值

c++ - 特征类、命名空间和前向声明

c++ - 聚合对象预期使用 '{...}' 进行初始化

c - 如何将数组放置在变量提供的特定内存位置?

vb.net - 在 vb.net 方法中使用 ByVal,通常的做法是什么?

java - 枚举:每个实例独有的方法

c++ - 无法使用 std::string 作为键迭代 std::map

c - 使用 memcpy 而不是 malloc 将空间存储到堆栈中