Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is special about numbers starting with zero?

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 ))

like image 360
Grigor Avatar asked Oct 25 '14 23:10

Grigor


People also ask

What are numbers that start with zero called?

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.

Why does a number not start with 0?

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 '.

Can a real number start with 0?

Real Numbers can also be positive, negative or zero.

What is the purpose of leading zeros?

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.)


1 Answers

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.

like image 174
Lightness Races in Orbit Avatar answered Oct 26 '22 17:10

Lightness Races in Orbit