Display of Octal numbers

Hi I am having problem in understanding of displaying octal numbers.Following is my code.


int main(int argc,char *argv[])

int octal=12;
int nonoctal=012;

printf("This number %o is octal of 12.\n",octal);
printf("This number %d is non octal.\n",nonoctal);
return 0;

Output displayed is

This number 14 is octal of 12
This number 10 is non octal

Here the first statement in output is fine.But why is the second statement gives output 10 instead of 012.

var octal ( 12 ) in decimal, convert to octal = 14

var nonoctal (012) in octal, convert to decimal = 10

its fine..
int nonoctal=012;
Here the compiler interprets the leading zero to mean that this number is in octal notation, so it becomes in decimal 1*8 + 2 = 10

If you want to display leading zeros, you need to specify that when using printf:
printf("This number %03d is padded with leading zeros.\n", 10);
thanks cronopio and Chervil.your replies are really helpful.:)
Topic archived. No new replies allowed.