When displaying the value of variable “int a = 011”, I get 9. Why? [duplicate]

这一生的挚爱 提交于 2019-12-01 22:20:28

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

zoska

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!