[英]What does the following code fragment (in C) print?
What does the following code fragment (in C) print? 以下代码片段(在C中)打印什么?
int a = 033;
printf("%d", a + 1);
033
is an octal integer literal and its value is 8*3+3 = 27
. 033
是八进制整数字面值 ,其值为8*3+3 = 27
。 Your code prints 28
. 你的代码打印
28
。
An integer literal that starts with a 0
is octal. 以
0
开头的整数文字是八进制。 If it starts in 0x
it's hexadecimal. 如果它以
0x
十六进制。
By the way, for an example's sake 顺便说一句,为了一个例子的缘故
int x = 08; //error
is a compile-time error since 8
is not an octal digit. 是一个编译时错误,因为
8
不是八进制数字。
我冒险疯狂猜测说28
:)
28. 28。
033 is an octal number in C because it has a leading "0" and that means its value is 27 in decimal. 033是C中的八进制数,因为它具有前导“0”,这意味着它的值为十进制的27。
So, 27 + 1 = 28 所以,27 + 1 = 28
here's a cue: 这是一个提示:
Try looking at this example: 试着看看这个例子:
#include<stdio.h>
main()
{
int a = 033;
printf("\nin decimal: %d", a+1);
printf("\nin hex: %x", a+1);
printf("\nin octal: %o", a+1);
}
this results in: 这导致:
in decimal: 28
in hex: 1c
in octal: 34
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.