Home >Backend Development >C++ >Why Do Leading Zeros Change Integer Values in C ?
Unveiling the Enigma of Leading Zeroes in Integers
The behavior you observed in Visual Studio 2013 is a reflection of the rules for representing integers in different bases, specifically decimal, octal, and hexadecimal.
An integer literal in C can start with zero to represent various bases:
In your example:
The compiler errors when attempting to assign 08 is because 8 is not a valid octal digit. It is only valid up to 7.
The reason for the conversion from 00016 to 14 is that the leading zeros in an octal literal indicate that the number is in base 8. The compiler performs the conversion by multiplying each digit by its corresponding power of 8, starting from right to left.
For 00016, this translates to:
0 * 8^4 0 * 8^3 0 * 8^2 1 * 8^1 6 * 8^0 = 14
Therefore, the behavior you observed is a result of the compiler's interpretation of the numeric values based on their leading digits and the rules for different bases in C .
The above is the detailed content of Why Do Leading Zeros Change Integer Values in C ?. For more information, please follow other related articles on the PHP Chinese website!