为什么变的printf的输出是不同的(Why printf output of the variab

2019-10-17 09:37发布

我不知道为什么它发生! 想知道原因。

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

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

没有任何人有答案吗?

Answer 1:

011 =八路,(1 * 8)+ 1 = 9 ........................



Answer 2:

011是一个八进制常数。 11 (b8) = 9 (b10)

C11(n1570),第6.4.4.1整型常量
一个八进制常数由前缀0可选地通过只7随后的数字0的序列组成。



Answer 3:

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


文章来源: Why printf output of the variable is different
标签: c printf