C Notes
Master relational (comparison) operators in C: equal to, not equal, greater than, less than, and their combinations. Learn how they return 0 or 1, common mistakes, and usage in conditions.
Relational operators — also called comparison operators — are what give your programs the ability to make decisions. Every if statement, every while loop, every conditional check relies on these operators to compare values and return a true or false result. In C, "true" is any non-zero value (typically 1), and "false" is 0.
List of Relational Operators in C
C provides six relational operators that compare two operands:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | 1 (true) |
!= | Not equal to | 5 != 3 | 1 (true) |
> | Greater than | 7 > 3 | 1 (true) |
< | Less than | 2 < 8 | 1 (true) |
>= | Greater than or equal to | 5 >= 5 | 1 (true) |
<= | Less than or equal to | 3 <= 2 | 0 (false) |
How Relational Operators Work
Every relational expression evaluates to either 1 (true) or 0 (false). This is important to understand — in C, there's no dedicated boolean type in C89 (C99 introduced _Bool), so relational expressions produce integer results.
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("a == b : %d\n", a == b);
printf("a != b : %d\n", a != b);
printf("a > b : %d\n", a > b);
printf("a < b : %d\n", a < b);
printf("a >= b : %d\n", a >= b);
printf("a <= b : %d\n", a <= b);
return 0;
}a == b : 0 a != b : 1 a > b : 0 a < b : 1 a >= b : 0 a <= b : 1
Equal To Operator (==)
The double equals == checks whether two values are identical. This is one of the most commonly used — and most commonly confused — operators in C.
#include <stdio.h>
int main() {
int x = 5;
float y = 5.0;
char ch = 'A';
printf("x == 5 : %d\n", x == 5);
printf("x == 10 : %d\n", x == 10);
printf("y == 5.0 : %d\n", y == 5.0);
printf("ch == 'A' : %d\n", ch == 'A');
printf("ch == 65 : %d\n", ch == 65); // 'A' is ASCII 65
return 0;
}x == 5 : 1 x == 10 : 0 y == 5.0 : 1 ch == 'A' : 1 ch == 65 : 1
The Classic Mistake: = vs ==
One of the most dangerous bugs in C is accidentally using assignment (=) instead of comparison (==):
#include <stdio.h>
int main() {
int x = 5;
// WRONG: This assigns 10 to x, then checks if 10 is non-zero (always true)
if (x = 10) {
printf("This always executes! x is now %d\n", x);
}
// CORRECT: This compares x with 10
x = 5;
if (x == 10) {
printf("This won't execute\n");
} else {
printf("x is not 10, x = %d\n", x);
}
return 0;
}This always executes! x is now 10 x is not 10, x = 5
Pro tip: Some programmers write if (10 == x) instead of if (x == 10). This way, if you accidentally write =, the compiler will catch it because you can't assign to a constant. This is called a "Yoda condition."
Not Equal To Operator (!=)
The != operator returns true when two values are different:
#include <stdio.h>
int main() {
int password = 1234;
int attempt = 5678;
if (attempt != password) {
printf("Access denied! Wrong password.\n");
}
// Useful in loops
char ch = 'x';
printf("Character is ");
if (ch != 'y') {
printf("not 'y'\n");
}
return 0;
}Access denied! Wrong password. Character is not 'y'
Greater Than (>) and Less Than (<)
These operators establish ordering between values:
#include <stdio.h>
int main() {
int age = 17;
float temperature = 38.5;
if (age < 18) {
printf("You are a minor (age: %d)\n", age);
}
if (temperature > 37.0) {
printf("You have a fever! (%.1f°C)\n", temperature);
}
// Finding the larger of two numbers
int a = 42, b = 67;
int max = (a > b) ? a : b;
printf("Larger number: %d\n", max);
return 0;
}You are a minor (age: 17) You have a fever! (38.5°C) Larger number: 67
Greater Than or Equal (>=) and Less Than or Equal (<=)
These inclusive comparison operators are essential for range checking:
Grade: C 50 is in the range [1, 100]
Comparing Different Data Types
When you compare values of different types, C applies type promotion rules — the smaller type gets promoted to the larger type before comparison:
#include <stdio.h>
int main() {
int i = 10;
float f = 10.5;
char c = 'A'; // 65
printf("int vs float: %d > %f is %d\n", i, f, i > f);
printf("char vs int: '%c' == %d is %d\n", c, 65, c == 65);
printf("char comparison: 'a' > 'A' is %d\n", 'a' > 'A');
return 0;
}int vs float: 10 > 10.500000 is 0 char vs int: 'A' == 65 is 1 char comparison: 'a' > 'A' is 1
Floating-Point Comparison Pitfall
Comparing floating-point numbers with == is unreliable due to precision issues:
#include <stdio.h>
#include <math.h>
int main() {
float a = 0.1 + 0.2;
float b = 0.3;
printf("0.1 + 0.2 = %.20f\n", a);
printf("0.3 = %.20f\n", b);
printf("Are they equal? %d\n", a == b);
// Safe comparison using epsilon
float epsilon = 0.00001;
if (fabs(a - b) < epsilon) {
printf("They are approximately equal\n");
}
return 0;
}0.1 + 0.2 = 0.30000001192092896000 0.3 = 0.30000001192092896000 Are they equal? 1 They are approximately equal
Best Practice: Never use == to compare floating-point numbers directly. Instead, check if the absolute difference is smaller than a tiny threshold (epsilon).
Relational Operators in Loops
Relational operators are the backbone of loop conditions:
Counting: 1 2 3 4 5 10 8 6 4 2 0
Chaining Comparisons — A Common Trap
Unlike mathematics, you cannot chain comparisons in C. Writing a < b < c does NOT mean "b is between a and c":
#include <stdio.h>
int main() {
int a = 1, b = 5, c = 3;
// This does NOT check if b is between a and c
// It evaluates as (a < b) < c → (1) < 3 → 1
if (a < b < c) {
printf("Misleading: This is TRUE but wrong logic!\n");
}
// Correct way to check range
if (a < b && b < c) {
printf("b is between a and c\n");
} else {
printf("b is NOT between a and c\n");
}
return 0;
}Misleading: This is TRUE but wrong logic! b is NOT between a and c
Precedence of Relational Operators
| Priority | Operators | Associativity |
|---|---|---|
| Higher | <, <=, >, >= | Left to Right |
| Lower | ==, != | Left to Right |
Relational operators have lower precedence than arithmetic operators, so a + b > c * d is evaluated as (a + b) > (c * d).
Interview Questions
Q1: What is the return type of relational expressions in C?
Relational expressions in C return an integer value — 1 for true and 0 for false. There's no built-in boolean type in C89. C99 introduced _Bool and stdbool.h with true/false macros, but internally they're still integers.
Q2: What's wrong with if (x = 5) and how does it differ from if (x == 5)?
if (x = 5) is an assignment, not a comparison. It assigns 5 to x, and since 5 is non-zero, the condition is always true. if (x == 5) correctly compares x with 5. This is one of the most common bugs in C programs.
Q3: Why should you avoid using == to compare floating-point numbers?
Floating-point numbers have limited precision and may not store exact values. Due to rounding errors, 0.1 + 0.2 might not exactly equal 0.3. Instead, use epsilon comparison: fabs(a - b) < epsilon.
Q4: What is the output of printf("%d", 3 > 2 > 1)?
The output is 0. The expression evaluates left to right: 3 > 2 gives 1, then 1 > 1 gives 0. This demonstrates why chaining comparisons doesn't work as expected in C.
Q5: What is the precedence relationship between arithmetic and relational operators?
Arithmetic operators (+, -, *, /, %) have higher precedence than relational operators. So in 5 + 3 > 6, the addition is performed first: 8 > 6 evaluates to 1.
Summary
- Relational operators compare two values and return
1(true) or0(false) - The
==operator checks equality — never confuse it with=(assignment) - Floating-point comparisons with
==are unreliable due to precision issues - You cannot chain comparisons like
a < b < c— use logical AND instead - Relational operators have lower precedence than arithmetic operators
- These operators are fundamental to decision-making (if/else) and loops (while/for)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Relational Operators in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, operators, relational, relational operators in c programming
Related C Programming Topics