What does the following code fragment (in C) print?
int a = 033;
printf("%d", a + 1);
033
is an octal integer literal and its value is 8*3+3 = 27
. Your code prints 28
.
An integer literal that starts with a 0
is octal. If it starts in 0x
it's hexadecimal.
By the way, for an example's sake
int x = 08; //error
is a compile-time error since 8
is not an octal digit.
我冒险疯狂猜测说28
:)
28.
033 is an octal number in C because it has a leading "0" and that means its value is 27 in decimal.
So, 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.