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

c decimal octal

2607 просмотра

3 ответа

With this code snippet:

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

Why is the result

a = 9

Автор: Ivan Gandacov Источник Размещён: 12.11.2019 09:52

Ответы (3)


12 плюса

Решение

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.

Автор: haccks Размещён: 03.06.2015 09:25

10 плюса

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 digits 0 through 7 only.

Автор: Sourav Ghosh Размещён: 03.06.2015 09:25

8 плюса

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.

Автор: zoska Размещён: 03.06.2015 09:26
Вопросы из категории :
32x32