62

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?

Community
  • 1
  • 1
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • 83
    It doesn't matter. It is zero in octal, decimal, binary, base 42... – juanchopanza Oct 29 '14 at 08:03
  • 2
    Ok @juanchopanza. Why is it different in different standards? what is it in C? – Gibbs Oct 29 '14 at 08:04
  • 7
    It's different in different standards because it happened to be easier to write the standard that way. It has no practical meaning. – Art Oct 29 '14 at 08:07
  • 8
    @user3801433 The "why" is simply "because the standard authors wrote it that way". Since it *really doesn't matter*, I don't think it's worth trying to "get right" or care much about. The bits will be the same in either case, which is what matters. – unwind Oct 29 '14 at 08:07
  • 2
    @juanchopanza: For your lack of faith, Captain Blackbeard awards you zero pieces-of-eight. – Kerrek SB Oct 29 '14 at 08:08
  • @unwind In the implementation, you're absolutely right. Since OP mentions an an interviewer, this could still be a good question along the lines of, "So OP, say there's some edge case with strange behavior. How would you go about finding out whether it's specified or not?" The interviewer might have a secret checklist to see what approaches the interviewee takes: "(i) tests in several implementations; (ii) looks at spec (ii.a) seems to be familiar with reading specs; ...", etc. – Joshua Taylor Oct 29 '14 at 10:23
  • 4
    @JoshuaTaylor "Post it at SO" ! – TobiMcNamobi Oct 29 '14 at 11:08
  • 2
    @Jongware, Read my description and check my link. Same link i have referred. It doesnt say about C standards – Gibbs Oct 29 '14 at 11:10
  • 3
    The C standard specifies only observable behaviour, and whether `0` is octal or decimal is not observable. So I'd say it's unspecified. (No matter what the grammar says, if your compiler treats `0` as decimal, it can still be a conforming implementation.) – mafso Oct 29 '14 at 12:05
  • Given that C++ is backward compatible with C, there is no reason why it should differ between the two languages. – Jan Hudec Oct 29 '14 at 19:34
  • 5
    @JanHudec This is a bit off topic, but C++ isn't backwards compatible with C. `int class = 5;`. I believe Objective-C is though. – David Young Oct 30 '14 at 02:54
  • 2
    @DavidYoung: Well, it's "almost" backwards compatible for a _useful_ value of "almost". – Jan Hudec Oct 30 '14 at 05:38
  • 1
    A numeric literal beginning with `0` is either octal or hex. If there is an `x` immediately after the first `0` then it's hex. If the first `0` is followed only by other digits then it's octal. If the numeric literal begins with any other digit it's decimal. This is true for both C and C++. – Hot Licks Oct 30 '14 at 15:13
  • It is neither. It is simply zero. – Dawood ibn Kareem Oct 30 '14 at 18:22
  • 0 seems to be the `NULL` of mathematics. It violates many basic rules, results in unexpected or ambiguous cases, and edge cases. Fun stuff! Great question! – Brandon Oct 30 '14 at 21:15
  • I am curious to know answers of "Will it make any difference? Why are they different in different standards?" .. – Amit Oct 31 '14 at 08:25
  • @Amit - will it make a difference? Yes, it could: (a simple purely mathematical example) `0 + 4 + 4` would give you 8 in decimal, and 10 in octal. And there could be a whole manner of strange situations that lead to a calculation that approximates the example - you could call `++` on a variable 8 times in a loop and if the start is 0 in octal, you would get 10. And you might not even spot the problem - consider looping and fetching `arr[myVar++]` and your termination is at, say, `10` - since you're not going out of bounds, you would get only 8 items out of it and skip index 8 and 9. – VLAZ Oct 31 '14 at 10:44
  • @Vld: no it doesn't work that way. Once compiled, the machine code doesn't even know or care whether an int is octal or decimal. – Lie Ryan Oct 31 '14 at 13:27

6 Answers6

94

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 3
    @juanchopanza "Will it make any difference? Why are they different in different standards?" are still unanswered. Please share if you have any information – Amit Oct 31 '14 at 08:26
  • @juanchopanza Since it's octal, does that not mean that certain compilers (I'm thinking for example of avr-gcc for microcontrollers) can optimize "0" into a single byte? Whereas if it was decimal, perhaps it would be the same size as an int? – Mike S Sep 15 '15 at 14:09
  • @MikeS It wouldn't make difference. The literal is still of type `int`. The compiler can do whatever the standard allows it, regardless of the base of the literal. – juanchopanza Sep 15 '15 at 14:45
59

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
28

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
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • 1
    Interesting. So it is considered as octal also in java!! – Gibbs Oct 29 '14 at 10:36
  • 14
    @user3801433 _"So it is considered as octal also in java"_ Not exactly: from that grammar, a bare `0` is considered as `DecimalNumeral`. But `00` is considered as `OctalNumeral`... – Sylvain Leroux Oct 29 '14 at 10:40
10

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Even there is no "hexadecimal" type, a constant which expresses a particular value in hex may be a different type from one which expresses the same value in decimal. For example, if `int` is 16 bits, then `0x8000` will be of type `unsigned int` but `32768` will be of type `long`. I don't think any such issues are meaningful for a constant zero, but for larger values they certainly are. – supercat Oct 29 '14 at 18:31
10

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:

  1. 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.
starrify
  • 14,307
  • 5
  • 33
  • 50
  • Your list doesn't include the terminator `digit`, which is required to fully specify `decimal-constant`. – Matt Mills Oct 29 '14 at 15:58
  • @arootbeer While you are true, it is not needed here as the key point is that `0` is listed explicitly under `octal-constant`. – glglgl Oct 30 '14 at 08:41
  • @glglgl Your statement is correct, but I wasn't trying to argue that this answer doesn't address the key point, simply to offer an idea for improvement. I just went and looked at the standard, and @starrify copied that section character for charcter; the definition for `digit` is actually in §6.4.2.1 - General Identifiers. – Matt Mills Oct 30 '14 at 13:36
-2

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" :)

i486
  • 6,491
  • 4
  • 24
  • 41
  • 1
    It doesn't depend on the compiler's implementation. It's defined in the standard as an octal constant. See the other answers. – PC Luddite Aug 06 '15 at 06:42
  • You can read answers above: "...Interestingly enough, from that grammar: •0 is decimal •but 00 is octal" – i486 Aug 09 '15 at 18:27
  • 1
    You do understand though that the answers all say that whether or not `0` is octal is *not* implementation dependent. It's defined in the standard as an octal constant. – PC Luddite Aug 09 '15 at 21:53