Search code examples
c++cliteralsoctal

Print Integer in C/C++


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 ?


Solution

  • 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