Is 0 an octal or a decimal in C?

CLanguage LawyerLiteralsOctal

C Problem Overview


I have read this. It's octal in C++ and decimal in Java. But no description about C?

Is it going to make any difference if 0 is octal or decimal? This is the question asked by my interviewer. I said no and I explained that it is always 0 regardless whether it is octal or decimal.

Then he asked why is it considered as octal in C++ and decimal in Java. I said it's the standard. Please let me know what is it in C? Will it make any difference? Why are they different in different standards?

C Solutions


Solution 1 - C

It makes little difference, but formally the integer constant 0 is octal in C. From the C99 and C11 standards, 6.4.4.1 Integer constants

> integer-constant:
>     decimal-constant integer-suffixopt
>     octal-constant integer-suffixopt
>     hexadecimal-constant integer-suffixopt
> > decimal-constant:
>     nonzero-digit
>     decimal-constant digit
> > octal-constant:
>     0
>     octal-constant octal-digit
> > hexadecimal-constant:
>     ...
>     ...

Solution 2 - C

Octal.

>###C11 §6.4.4.1 Integer constants

> octal-constant: 0 octal-constant octal-digit

And this is true since C89 §3.1.3.2.

Solution 3 - C

> Then he asked why is it considered as octal in C++ and decimal in Java

For sake of completeness, worth mentioning Java specs as well. From Java Language Specification 3.10.1:

> > > DecimalNumeral: > 0 > NonZeroDigit Digitsopt > NonZeroDigit Underscores Digits > > A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9 interspersed with underscores, representing a positive integer.

> > > OctalNumeral: > 0 OctalDigits > 0 Underscores OctalDigits > > An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

As you can see, a bare 0 is considered as decimal. Whereas any (non-empty) sequence of digits preceded by 0 is considered as octal.

Interestingly enough, from that grammar:

  • 0 is decimal
  • but 00 is octal

Solution 4 - C

From the C Standard (6.4.4.1 Integer constants)

octal-constant:
0
octal-constant octal-digit

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.

Solution 5 - C

It's an octal. See section 6.4.4.1 Integer constants of the N1570 draft:

      integer-constant:
            decimal-constant integer-suffixopt
            octal-constant integer-suffixopt
            hexadecimal-constant integer-suffixopt
      decimal-constant:
            nonzero-digit
            decimal-constant digit
      octal-constant:
            0
            octal-constant octal-digit
      hexadecimal-constant:
            hexadecimal-prefix hexadecimal-digit
            hexadecimal-constant hexadecimal-digit
      hexadecimal-prefix: one of
            0x   0X
      nonzero-digit: one of
            1   2   3   4   5   6   7   8   9
      octal-digit: one of
            0   1   2   3   4   5   6   7
      hexadecimal-digit: one of
            0   1   2   3   4   5   6   7   8   9
            a   b   c   d   e   f
            A   B   C   D   E   F
      integer-suffix:
            unsigned-suffix long-suffixopt
            unsigned-suffix long-long-suffix
            long-suffix unsigned-suffixopt
            long-long-suffix unsigned-suffixopt
      unsigned-suffix: one of
            u   U
      long-suffix: one of
            l   L
      long-long-suffix: one of
            ll   LL

Also:

> 3. A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only. A hexadecimal constant consists of the prefix 0x or 0X followed by a sequence of the decimal digits and the letters a (or A) through f (or F) with values 10 through 15 respectively.

Solution 6 - C

I think it depends on compiler implementation. We have to see the source code to determine whether it flags a "0" constant as octal or not. I can define the non-octal reason in this way: Octals has "0" prefix. But there is no prefix. If the constant is 00, then it IS octal - "octal Zero" :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGibbsView Question on Stackoverflow
Solution 1 - CjuanchopanzaView Answer on Stackoverflow
Solution 2 - CYu HaoView Answer on Stackoverflow
Solution 3 - CSylvain LerouxView Answer on Stackoverflow
Solution 4 - CVlad from MoscowView Answer on Stackoverflow
Solution 5 - CstarrifyView Answer on Stackoverflow
Solution 6 - Ci486View Answer on Stackoverflow