HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Is 0 a decimal literal or an octal literal?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
literaldecimaloctal

Problem

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.

Solution

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                           
    octal-literal octal-digit

Code Snippets

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

Context

Stack Overflow Q#6895522, score: 317

Revisions (0)

No revisions yet.