Example 1 — Precedence and the bitwise trap
== binds tighter than &, which surprises many beginners.
#include <stdio.h>
int main(void) {
int r1 = 2 + 3 * 4;
int r2 = (2 + 3) * 4;
int r3 = 10 - 4 - 3;
int a, b;
a = b = 7;
int flags = 6;
int wrong = flags & 4 == 4;
int right = (flags & 4) == 4;
printf("%d %d %d\n", r1, r2, r3);
printf("a=%d b=%d\n", a, b);
printf("wrong=%d right=%d\n", wrong, right);
return 0;
} How It Works
*runs before+; parentheses override that.-is left associative:(10 - 4) - 3.flags & 4 == 4isflags & (4 == 4)=6 & 1= 0.- Parentheses make the intended
(flags & 4) == 4evaluate to 1.
