int n = 00000011;
printf("n is: %d\n", n);
x is: 9
Shouldn't it be 3 in decimal?
int n = 00000011;
printf("n is: %d\n", n);
x is: 9
Shouldn't it be 3 in decimal?
00000011
is the octal value of 9
, you can't use the binary representation directly.
Octal 011 -> Decimal 9
Only, decimal, octal and hexadecimal representations can be specified, for the decimal representation it's straight forward, for the octal representation you prefix the value with a 0
so 09
would not be valid, and for hex representation you prefix the value with 0x
.
note: as commented by abligh, you can use the 0b
prefix with gcc
and clang
and probably other compilers.
In c number literals starting with 0*digit* are base-8. For binary some compilers allow you to start them with 0b. else you'll have to manually convert them to hex or octal.
#include <stdio.h>
int main(void){
int n=0b00000011;
printf ("n is %d\n",n);
return 0;
};