The question of whether 0 is decimal or octal in C++

  • 2020-04-01 21:35:52
  • OfStack

Although in any base number there is only one value for zero -- and that is zero -- the question is simply a pain in the ass.

In a recent conversation with friends about Numbers, he said octal Numbers are now almost never used. I can't immediately refute the fact that, in addition to the decimal system, which we often use, the binary and hexadecimal systems are the most commonly used in computers, and octal Numbers are indeed "niche".

When I got home and thought about it, octal, octal, "octal is the number that starts with zero," hey, does that zero count? Zero is also a number that starts with zero. If 0 is an octal number, we use octal Numbers almost every day. There's a lot to learn about computers, simple types aren't simple at all, and more painful computer problems can be found in my code mysteries series.

So is 0 an octal number? How is the C++ specification defined?

Yes, in C++, 0 is octal.

According to the C++ standard:

2.14.2 integer [lex icon]

An integer literal:  
      A decimal - literal integer - suffixopt 
      Octal -- literal integer - suffixopt 
      Hexadecimal - literal integer - suffixopt 
A decimal - literal:  
      Nonzero - digit 
      A decimal - literal digit 
Octal - literal:  
      0                                       < -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- < Here >
      Octal -- literal octal digit

Later we'll be able to say proudly, "in computers, octal is more common than binary, or even hexadecimal."

 


Related articles: