Print Integer in C/C++

adrian008 picture adrian008 · Jun 26, 2015 · Viewed 7.7k times · Source

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 ?

Answer

Ashot picture Ashot · Jun 26, 2015

Numbers starting with 0 are octal in c/c++.

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