1
  #include <stdio.h>
  #include <stdlib.h>

  #define SIZEOF(arr) (sizeof(arr) / sizeof(arr[0]))
  #define PrintInt(expr) printf("%s:%d\n",#expr,(expr))

  int main()
  {
      /* The powers of 10 */
      int pot[] = {
          0001,
          0010,
          0100,
          1000
      };
      int i;

      for(i=0;i<SIZEOF(pot);i++)
          PrintInt(pot[i]);
      return 0;
  }

The output of the following code is

pot[i]:1
pot[i]:8
pot[i]:64
pot[i]:1000

why does it give such output??`

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
CODER
  • 11
  • 3

3 Answers3

9

Prefixing a numeric literal in C with a 0 digit will output it in Octal, which is a base 8 numeric system.

Oct(1) = Dec(1)

Oct(10) = Dec(8)

Oct(100) = Dec(64)

That's where your numbers are coming from.

FYI, Hexadecimal literals are prefixed with 0x and binary literals are prefixed with 0b (IIRC)

Edit: To actually answer your question, just remove the leading zeroes from the numbers and it should give you the desired output.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57
E. Moffat
  • 3,165
  • 1
  • 21
  • 34
  • 4
    `0b` for binary literals is non-standard. (Interestingly, `0` is an octal literal; there is no decimal integer literal whose value is zero.) – Keith Thompson Aug 08 '14 at 17:13
  • @KeithThompson - That is an intersting point about 0! Never thought about it that way. – E. Moffat Aug 08 '14 at 17:15
  • @KeithThompson Interesting, are we sure that isn't `00` the octal zero? – DSquare Aug 08 '14 at 17:17
  • @DSquare: What do you mean by *the* octal zero? Both `0` and `00` (as well as `000` and `0000`, etc.) are octal integer constants with the value zero. – Keith Thompson Aug 08 '14 at 17:18
  • @KeithThompson Could we call `00` (and `0...0`) an octal literal for 0 and `0` the decimal literal for 0? Since the first 0 might not actually be parsed and used just as a flag and thus `0` not having the "octal prefix" because it can't be the prefix of *nothing*. – DSquare Aug 08 '14 at 17:19
  • Apparently not http://stackoverflow.com/questions/6895522/is-0-a-decimal-literal-or-an-octal-literal – DSquare Aug 08 '14 at 17:25
  • @DSquare: No. The C syntax for integer constants is unambiguous; all octal constants start with the character `0`, and all decimal constants start with one of the characters `1` through `9`.. `0` and `00` are both octal constants. (`0` happens to have the same value that it would have *if* the syntax treated it as a decimal constant, but it doesn't.) It rarely *matters* whether `0` is octal or decimal, but the simple fact is that it's octal. See [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 6.4.4.1. – Keith Thompson Aug 08 '14 at 17:26
  • @Keith: it may be noted that not all integer constants that start with `0` are *therefore* octal: `0x` is the prefix for hexadecimal. (Fortunately, the standard is clear on that: `0` is *the* prefix for octal, and `0x` is *the* prefix for hex.) – Jongware Aug 08 '14 at 17:33
2

Number literals which start with a zero are interpreted as being in octal – base 8. So the first three numbers are octal 1, 10 and 100 (which are 1, 8 and 64 in base-10); the last number is base 10.

Norman Gray
  • 11,978
  • 2
  • 33
  • 56
-1

Based on how your constants are defined, and how your question is worded, I think you want hex. Prefix your values with "0x":


int pot[] = {
    0x0001,
    0x0010,
    0x0100,
    0x1000
;
Don Shankin
  • 440
  • 3
  • 10