Relational operators in C are used to compare two values or expressions. They play a crucial role in decision-making, especially in if, while, for, and other conditional statements.
A relational operator always returns:
- 1 (true) → if the relationship is correct
- 0 (false) → if the relationship is incorrect
C provides six relational operators.
List of Relational Operators
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
1. Equal To ( == )
Checks if two values are equal.
Example
int a = 10, b = 10;
printf("%d", a == b); // Output: 1
2. Not Equal To ( != )
Checks if two values are not equal.
Example
int a = 10, b = 20;
printf("%d", a != b); // Output: 1
3. Greater Than ( > )
Checks if the left operand is greater than the right.
Example
int a = 15, b = 8;
printf("%d", a > b); // Output: 1
4. Less Than ( < )
Checks if the left operand is smaller than the right.
Example
int a = 5, b = 12;
printf("%d", a < b); // Output: 1
5. Greater Than or Equal To ( >= )
True when the left operand is greater than or equal to the right.
Example
int a = 25, b = 25;
printf("%d", a >= b); // Output: 1
6. Less Than or Equal To ( <= )
True when the left operand is smaller than or equal to the right.
Example
int a = 10, b = 20;
printf("%d", a <= b); // Output: 1
Combined Example Using All Relational Operators
#include <stdio.h>
int main() {
int x = 10, y = 20;
printf("x == y : %d\n", x == y);
printf("x != y : %d\n", x != y);
printf("x > y : %d\n", x > y);
printf("x < y : %d\n", x < y);
printf("x >= y : %d\n", x >= y);
printf("x <= y : %d\n", x <= y);
return 0;
}
Using Relational Operators in Conditions
Example: Find the larger number
int a = 50, b = 30;
if (a > b) {
printf("a is greater");
}
Example: Check eligibility
int age = 18;
if (age >= 18) {
printf("Eligible to vote");
}
Important Points to Remember
✔ Relational operators return 0 or 1
✔ Can be used with numbers, characters, and expressions
✔ Used in loops and conditional statements
✔ Comparison does not modify values
✔ == is comparison; = is assignment (students often confuse this)
Real-World Example
Check if a student has passed
int marks = 45;
if (marks >= 33) {
printf("Pass");
} else {
printf("Fail");
}
Conclusion
Relational operators are essential in C for comparing values and controlling program flow. From simple comparisons to complex decision-making structures, these operators form the backbone of logical evaluation in C programs.
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming