At compile time, the C compiler identifies any integer literals in your code, and then interprets them using a set of rules to get their binary value for use by your program:
- Base-16 (hexadecimal) - Any integer literals starting with '0x' will be treated as a hexadecimal value. So
int x = 0x22
gives x
decimal value 2 * 16^1 + 2 * 16^0 = 34
. - Base-8 (Octal) - Any integer literals starting with '0' will be treated as octal values. So
int x = 022
gives x
decimal value 2 * 8^1 + 2 * 8^0 = 18
. - Base-10 (Decimal) - any integer literals that do not match the other two rules will be treated as a decimal value. So
int x = 22
gives x
decimal value of 22
.
It should be noted that GCC supports an extension that provides another rule for specifying binary integers. In addition, these specification methods are supported only for integer literals at compile time.
source share