When displaying the value of the variable "int a = 011" I get 9. Why?

With this piece of code:

int a = 011;
printf("a = %d", a);

      

Why the result

a = 9

+3


source to share


3 answers


011

is an octal value, and its decimal equivalent is 9. The preceding integer literal c 0

indicates an octal value.
Use %o

the in specifier printf

to print the value in octal format.



+12


source


Leading 0

in a int

literal constant or int

representing an 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 a prefix 0

, optionally followed by a sequence of numbers 0

only 7

.

+10


source


With 0

at the beginning of a numeric literal, you specify the octal system. And 11

in octal system 1*8 + 1 = 9

.

+7


source







All Articles