Learn C
#include "operators/op_comparison.h"

Comparison Operators in C

#concept

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.

#how it works
1

Six operators, int results

== != < <= > >= each yield 1 or 0. They chain into conditions like low <= x && x <= high.

2

= vs ==

if (x = 5) assigns and tests 5 (always true) instead of comparing — -Wall warns, and (5 == x) style makes typos fail loudly.

3

Never == on doubles

0.1 + 0.2 != 0.3 in binary floating point. Test fabs(a - b) < 1e-9 instead of exact equality.

4

Comparisons in arithmetic

Because results are ints, (a > b) + (c > d) counts how many held — occasionally useful, usually obscure.

#example
#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;
}
Output
1
1
1
1score >= 60 is true (85 passes), printing 1 since comparisons yield ints.
2score == 85 compares rather than assigns, correctly yielding true.
3score != 100 is also true — all three comparisons agree.
#use cases
Gatekeeping branches and loop exits on threshold checks
Sorting with qsort comparators returning negative/zero/positive
Guarding floating-point tests with epsilon comparisons
Counting true conditions by summing comparison results

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.