This question already has an answer here:
With this code snippet:
int a = 011;
printf("a = %d", a);
Why is the result
a = 9
011
is an octal value and its decimal equivalent is 9. Preceding integer literal with 0
indicates octal value.
Use %o
specifier in printf
to print the value in octal.
A leading 0
, in an int
literal or int
constant, represents the octal value. It is called an octal constant.
Related: C11
standard, chapter 6.4.4.1, Integer constants, Paragraph 3,
An octal constant consists of the prefix
0
optionally followed by a sequence of the digits0
through7
only.
With 0
at the beginning of of a numeric literal, you specify the octal system. And 11
in the octal system is 1*8 + 1 = 9
.
来源:https://stackoverflow.com/questions/30615960/when-displaying-the-value-of-variable-int-a-011-i-get-9-why