Operators
Prerequisites: Type casting & conversion
C supports the usual arithmetic (+ - * / % ++ --), comparison
(== != > < >= <=), logical (&& || !), and — used more often than
in most languages — bitwise (& | ^ ~ << >>) operators.
int a = 7, b = 2;
printf("%d\n", a % b); // 1 — remainder
int flags = 0b0110;
int masked = flags & 0b0010; // bitwise AND
printf("%d\n", masked); // 2
Common mistakes
- Confusing
&&/||(logical) with&/|(bitwise) — using a single&where&&was meant compiles fine but silently does something completely different. - Writing
if (x = 5)instead ofif (x == 5)— this is legal C (it assigns5tox, and the condition is then always true), and one of the most notorious C beginner bugs. Most compilers warn about this if you enable warnings (-Wall).