0
#include<stdio.h>
void main(){
  int i;
  i = 011;
  printf("%d",i);
}

This program gives output as 9. I don't know the reason for that. Please help me to figure out why this program is giving this output.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Vishvendra Singh
  • 195
  • 2
  • 13

3 Answers3

7

In C, you can represent the value 9 by:

  • Hexadecimal (Base 16): 0x9
  • Decimal (Base 10): 9
  • Octal (Base 8): 011
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
5

In C/C++/Java, hex numbers begin with 0x. Octal numbers begin with 0.

011 is octal of 9

P0W
  • 46,614
  • 9
  • 72
  • 119
  • And before you ask why, read [this](http://stackoverflow.com/q/1835465/1870232) – P0W Jan 04 '14 at 05:55
1

011 is octal number due to 0 preceding it

011 = 1*(8^1) + 1*(8^0)
    = 1*8 + 1*1
    = 8 +1
    = 9 in decimal(%d)

0x for hex and only digits(i.e 9) for decimal

riteshtch
  • 8,629
  • 4
  • 25
  • 38