printf("%i\n",2&2==2);
This should print out a 1 but I get a 0, why is this?
int ans=2&2;
printf("%i\n",ans==2);
This prints a 1, how come the first way does not work? This is the case with if statements as well
The order of operations is different than you think it is. A correct way to write it in a single line would be:
printf("%i\n", (2 & 2) == 2); // Prints 1
@BackSpace7777777: if you're not familiar with "operator precedence", see here: tutorialspoint.com/cprogramming/c_operators_precedence.htm
@BackSpace7777777:
&
and&&
used to be one context-sensitive operator. The weird precedence of&
is an artifact of that history.