CRelational Operators

Relational Operators

Relational operators compare two values and answer a yes/no question: is one value equal to, greater than, or less than another? C provides six of them: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).

C
#include <stdio.h>

int main(void) {
    int a = 5, b = 8;

    printf("%d\n", a == b); // 0
    printf("%d\n", a != b); // 1
    printf("%d\n", a > b);  // 0
    printf("%d\n", a < b);  // 1
    printf("%d\n", a >= 5); // 1
    printf("%d\n", b <= 8); // 1
    return 0;
}
Relational Expressions Are Just int
No dedicated boolean type in classic C
Every relational (and logical) expression in C evaluates to an int: 1 for true, 0 for false. There is no built-in bool keyword in classic C — you can assign the result of a comparison directly to an int variable. C99 added _Bool and the <stdbool.h> header, which gives you the friendlier names bool, true, and false as macros over _Bool, but under the hood it is still a small integer type.

C
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    int isEqual = (5 == 5); // isEqual is 1
    bool isGreater = (10 > 3); // true, which prints as 1

    printf("%d %d\n", isEqual, isGreater);
    return 0;
}
Comparing Floating-Point Numbers
Never compare floats with == directly
Floating-point numbers are stored in binary and cannot represent most decimal fractions exactly. Two values that look equal in source code (like the result of 0.1 + 0.2) can differ by a tiny amount after rounding, so == silently returns false. The standard fix is to compare against a small tolerance ("epsilon") instead of exact equality.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double a = 0.1 + 0.2;
    double b = 0.3;

    printf("%d\n", a == b);              // 0 — surprising, but expected!
    printf("%d\n", fabs(a - b) < 1e-9);  // 1 — correct way to compare floats
    return 0;
}
Chained Comparisons Do Not Work Like Math
1 < x < 10 does not mean what it looks like
In mathematics, 1 < x < 10 means "x is between 1 and 10". In C, relational operators are left-associative and each comparison produces an int (0 or 1), which then feeds into the next comparison. So 1 < x < 10 is actually evaluated as (1 < x) < 10. Since (1 < x) is always either 0 or 1, and both 0 and 1 are less than 10, this expression is always true — regardless of what x actually is. This is one of the most dangerous gotchas for people coming from math or other languages.

C
int x = 500;

// BROKEN: always true, no matter what x is!
if (1 < x < 10) {
    printf("this always runs, even though x is 500\n");
}

// CORRECT: combine two separate comparisons with &&
if (x > 1 && x < 10) {
    printf("x is genuinely between 1 and 10\n");
} else {
    printf("x is out of range\n");
}
  • ==, !=, >, <, >=, <= all produce an int result: 1 or 0

  • Classic C has no boolean type; use <stdbool.h> (C99+) for readability

  • Never compare float/double values with == — use an epsilon-based comparison

  • C has no chained comparisons — 1 < x < 10 does not check a range, always combine with &&