Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 0 a decimal literal or an octal literal?

Tags:

c++

zero

octal

Zero is always zero, so it doesn't matter. But in a recent discussion with a friend he said that octal literals are almost unused today. Then it dawned upon me that actually almost all integer literals in my code are octal, namely 0.

Is 0 an octal literal according to the C++ grammar? What does the standard say?

The only real use I'm aware of is for unix file permissions.

like image 883
Yakov Galka Avatar asked Aug 01 '11 07:08

Yakov Galka


People also ask

Is 0 octal or decimal?

In fact there is no any difference for zero because zero is a common digit for octal, decimal and hexadecimal numbers. It has meaning only when a number has other digits apart from the single (leading) zero. Take into account that there are no such integral types as decimal, octal or hexadecimal.

Is 0 an integer literal?

Integer literals beginning with the digit 0 are interpreted as an octal integer literal rather than as a decimal integer literal.

What is an octal literal?

An octal integer literal begins with the digit 0 and contains any of the digits 0 through 7.

Do octal numbers start with 0?

An integer literal that starts with 0 is an octal number, much like a number starting with 0x is a hexadecimal number.


2 Answers

Yes, 0 is an Octal literal in C++.

As per the C++ Standard:

2.14.2 Integer literals [lex.icon]

integer-literal:       decimal-literal integer-suffixopt       octal-literal integer-suffixopt       hexadecimal-literal integer-suffixopt   decimal-literal:       nonzero-digit       decimal-literal digit   octal-literal:       0                           <--------------------<Here>     octal-literal octal-digit 
like image 78
Alok Save Avatar answered Sep 18 '22 15:09

Alok Save


Any integer value prefixed with 0 is an octal value. I.e.: 01 is octal 1, 010 is octal 10, which is decimal 8, and 0 is octal 0 (which is decimal, and any other, 0).

So yes, '0' is an octal.

That's plain English translation of the grammar snippet in @Als's answer :-)


An integer prefixed with 0x is not prefixed with 0. 0x is an explicitly different prefix. Apparently there are people who cannot make this distinction.

As per that same standard, if we continue:

 integer-literal:      decimal-literal integer-suffixopt      octal-literal integer-suffixopt      hexadecimal-literal integer-suffixopt  decimal-literal:      nonzero-digit                       <<<---- That's the case of no prefix.      decimal-literal digit-separatoropt digit  octal-literal:      0                                    <<<---- '0' prefix defined here.      octal-literal digit-separatoropt octal-digit <<<---- No 'x' or 'X' is                                                           allowed here.  hexadecimal-literal:      0x hexadecimal-digit                 <<<---- '0x' prefix defined here      0X hexadecimal-digit                 <<<---- And here.      hexadecimal-literal digit-separatoropt hexadecimal-digit 
like image 44
littleadv Avatar answered Sep 21 '22 15:09

littleadv