This is kinda stupid question, but it's interesting for me )
This is what i get with 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...
If number starts with zero and contains 8, it's compile error. Is this normal? And what exactly compiler does with starting zeros if 00016 == 14?
Thanks to all ))
An integer literal that starts with 0 is an octal number, much like a number starting with 0x is a hexadecimal number. Octal numbers can only contain the digits 0 to 7 , and this is why you get a compilation error.
A number can't start with zeros, only if this number is zero itself. For example, 001 is not a valid number, but 0 is a valid number. A number can have a fractional part. A delimiter between an integer part and a fractional part can be either '.
Real Numbers can also be positive, negative or zero.
Leading zeros are used to make ascending order of numbers correspond with alphabetical order: e.g., 11 comes alphabetically before 2, but after 02. (See, e.g., ISO 8601.)
Yes, this is expected.
[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 ]22The digits 8 and 9 are not octal digits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With