Comparison Operators in C
Comparisons (==, !=, <, <=, >, >=) evaluate to int 1 (true) or 0 (false) in C — there is no distinct boolean result type at the operator level. They drive every if, loop condition, and assertion.
Two evergreen traps: assignment (=) where equality (==) was meant, which compiles silently inside if without -Wall, and comparing floating-point values for exact equality, which fails on rounding dust — compare with an epsilon (fabs(a - b) < 1e-9) instead.
Six operators, int results
== != < <= > >= each yield 1 or 0. They chain into conditions like low <= x && x <= high.
= vs ==
if (x = 5) assigns and tests 5 (always true) instead of comparing — -Wall warns, and (5 == x) style makes typos fail loudly.
Never == on doubles
0.1 + 0.2 != 0.3 in binary floating point. Test fabs(a - b) < 1e-9 instead of exact equality.
Comparisons in arithmetic
Because results are ints, (a > b) + (c > d) counts how many held — occasionally useful, usually obscure.
#include <stdio.h> int main(void) { int score = 85; printf("%d\n", score >= 60); printf("%d\n", score == 85); printf("%d\n", score != 100); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.