Iqbal
Iqbal

Reputation: 245

Why printf output of the variable is different

I could not know why it happen! Want to know the reason.

{
int i=01;
printf("%d\n",i);
}
output: 1

but

{
int i=011;
printf("%d\n",i);
}
output: 9

Does anybody have the answer?

Upvotes: 1

Views: 302

Answers (3)

Yash Tyagi
Yash Tyagi

Reputation: 1

The numbers which are preceded by 0 is called octal numbers in c programming .
to evaluate such an expression we simply follow a conversion rule of converting octal to decimal number system
For conversion the following steps are  to be proceed
such as 011
here 0 indicate the number is octal 
and we are require to convert 11 which is (base 8) to decimal (base 10)

11= 1x8^1+1x8^0
   =8+1
   =9

Upvotes: -1

Martin James
Martin James

Reputation: 24857

011 = Octal, (1*8)+1=9 ........................

Upvotes: 4

md5
md5

Reputation: 23699

011 is an octal constant. 11 (b8) = 9 (b10).

C11 (n1570), § 6.4.4.1 Integer constants
An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.

Upvotes: 11

Related Questions