Is 0 an octal or a decimal in C?

Gibbs picture Gibbs · Oct 29, 2014 · Viewed 7.4k times · Source

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?

Answer

juanchopanza picture juanchopanza · Oct 29, 2014

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