Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 0 an octal or a decimal in C? [duplicate]

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?

like image 598
Gibbs Avatar asked Oct 29 '14 08:10

Gibbs


People also ask

What is an octal integer in C?

An integer constant is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.

What is decimal constant C?

A decimal constant begins with a nonzero digit. For example, 255 is the decimal constant for the base-10 value 255. A number that begins with a leading zero is interpreted as an octal constant. Octal (or base eight) notation uses only the digits from 0 to 7.

What's the point of octal?

The main advantage of using Octal numbers is that it uses less digits than decimal and Hexadecimal number system. So, it has fewer computations and less computational errors. It uses only 3 bits to represent any digit in binary and easy to convert from octal to binary and vice-versa.


2 Answers

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

like image 74
juanchopanza Avatar answered Oct 01 '22 21:10

juanchopanza


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.

like image 38
Yu Hao Avatar answered Oct 01 '22 22:10

Yu Hao