为什么变量的printf输出是不同的

时间:2012-11-10 10:32:31

标签: c printf

我不知道为什么会这样!想知道原因。

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

但是

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

有人有答案吗?

3 个答案:

答案 0 :(得分:11)

011是八进制常量。 11 (b8) = 9 (b10)

  

C11(n1570),§6.4.4.1整数常量
  八进制常量由前缀0组成,可选地后跟一个数字0到7的序列。

答案 1 :(得分:4)

011 =八分之一,(1 * 8)+ 1 = 9 ........................

答案 2 :(得分:-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