Search code examples
coctalnumeral-system

Printing the value of integer data type using %x in printf statement


Got confused with the following peice of code.

#include <stdio.h>
void main()
{
int a=0100;
printf("%x",a);
}

The value I am getting is 40.

Can someone explain me whats going on here?

Note : When I removed the digit 0 before digit 1 then its coming 64 which is correct, when 100 is converted into hex.

Codepad link to above code


Solution

  • In C, a constant prefixed with a 0 is an octal constant. 0100 in base 8 is 1000000 in base 2, which is 40 in hexadecimal, which is 64 in base 10. So your program is printing exactly what it should.