Printing integers 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 must be wrong, but why 5349?

+3


source to share


2 answers


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

      

+11


source


A number beginning with one or more leading zeros indicates octal format instead of decimal. So 000012345 is 1 * 8 ^ 4 + 2 * 8 ^ 3 + 3 * 8 ^ 2 + 4 * 8 ^ 1 + 5 * 8 ^ 0 = 5349.



Likewise, a number starting with 0x is in hexadecimal format.

+2


source







All Articles