Relational (Comparison) Operators in C
Relational operators compare two values and return 1 (true) or 0 (false). They are used in if statements and loops to make decisions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal to | 5 != 3 | 1 (true) |
| > | Greater than | 10 > 7 | 1 (true) |
| < | Less than | 3 < 8 | 1 (true) |
| >= | Greater than or equal | 5 >= 5 | 1 (true) |
| <= | Less than or equal | 3 <= 2 | 0 (false) |
c
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 10;
printf("a=%d, b=%d, c=%d\n\n", a, b, c);
printf("a == b : %d\n", a == b); // 0 (false)
printf("a == c : %d\n", a == c); // 1 (true)
printf("a != b : %d\n", a != b); // 1 (true)
printf("a > b : %d\n", a > b); // 0 (false)
printf("a < b : %d\n", a < b); // 1 (true)
printf("a >= c : %d\n", a >= c); // 1 (true, equal)
printf("b <= a : %d\n", b <= a); // 0 (false)
// Practical use
int marks = 75;
if (marks >= 40)
printf("\nResult: PASS\n");
else
printf("\nResult: FAIL\n");
return 0;
}▶ Output
a=10, b=20, c=10 a == b : 0 a == c : 1 a != b : 1 a > b : 0 a < b : 1 a >= c : 1 b <= a : 0 Result: PASS
⚠ Warning: Do NOT confuse
== (comparison) with = (assignment)! Writing if (x = 5) instead of if (x == 5) is a classic bug — it assigns 5 to x and always evaluates to true!