I have a simple program.
#include <cstdio> int main() { int num = 000012345; printf("%d\n",num); return 0; }
The above program gives 5349. Why ? I mean it should be wrong, but why 5349 ?
Numbers starting with 0 are octal in c/c++.
0
Octal = 000012345 Decimal= 0×8⁸+0×8⁷+0×8⁶+0×8⁵+1×8⁴+2×8³+3×8²+4×8¹+5×8⁰ = 5349 Binary = 1010011100101 Hex = 14E5
A number starting with one or more leading zeros specifies octal format instead of decimal. So 000012345 is 1 * 8^4 + 2 * 8^3 + 3 * 8^2 + 4 * 8^1 + 5 * 8^0 = 5349.
Similarly, a number starting with 0x is in hexadecimal format.
Numbers starting with
0
are octal in c/c++.