这是个愚蠢的问题,但对我来说很有趣)
这是我用 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/