Understanding Numeric Constant Prefixes in C/C
When writing numeric constants in C/C , the prefixes used before the numbers carry significant meaning that can alter the interpreted value. One such prefix is '0', which represents an octal (base-8) constant.
Consider the case when the numeric constant '0123' was assigned to an integer variable. In base-10 (decimal), this value would indeed represent 123. However, because it's prefixed with '0', it's interpreted as an octal constant.
In the binary representation of an octal digit, each digit has a specific value:
As '0123' is an octal constant, we can convert each digit to its binary equivalent:
Concatenating these binary digits, we get: 000001001011. Converting this to base-10 (decimal) gives us the value 83.
Therefore, when a numeric constant in C/C is prefixed with a '0', it signifies an octal constant, and the value is interpreted accordingly. This behavior is consistent across both C and C with the GCC compiler and even for floating-point constants.
The above is the detailed content of Why does '0123' equal 83 in C/C ?. For more information, please follow other related articles on the PHP Chinese website!