Relational operators are used to compare two values or expressions in C.
They are commonly used in decision-making statements such as
if, if-else, and loops.
1
when the comparison is true and 0 when the comparison is false.
Relational operators compare two operands and determine the relationship between them.
int a = 10;
int b = 20;
printf("%d", a < b);
Since 10 is less than 20, the expression produces
1.
| 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 |
The == operator checks whether two values are equal.
int a = 10;
int b = 10;
printf("%d", a == b);
Output:
1
The result is 1 because both values are equal.
int a = 10;
int b = 20;
printf("%d", a == b);
Output:
0
The result is 0 because 10 and 20 are
not equal.
The != operator checks whether two values are different.
int a = 10;
int b = 20;
printf("%d", a != b);
Output:
1
The result is 1 because the values are different.
int a = 10;
int b = 10;
printf("%d", a != b);
Output:
0
The result is 0 because the values are equal.
The > operator checks whether the left operand is greater
than the right operand.
int a = 20;
int b = 10;
printf("%d", a > b);
Output:
1
int a = 10;
int b = 20;
printf("%d", a > b);
Output:
0
The expression is false because 10 is not greater than
20.
The < operator checks whether the left operand is less than
the right operand.
int a = 10;
int b = 20;
printf("%d", a < b);
Output:
1
int a = 30;
int b = 20;
printf("%d", a < b);
Output:
0
The expression is false because 30 is not less than
20.
The >= operator checks whether the left operand is greater
than or equal to the right operand.
int marks = 60;
printf("%d", marks >= 40);
Output:
1
The condition is true because 60 is greater than
40.
int marks = 40;
printf("%d", marks >= 40);
Output:
1
The condition is also true when both values are equal.
The <= operator checks whether the left operand is less than
or equal to the right operand.
int age = 18;
printf("%d", age <= 18);
Output:
1
int age = 15;
printf("%d", age <= 18);
Output:
1
The condition is true because 15 is less than
18.
In C, a relational expression evaluates to an int value:
1 for true and 0 for false.
printf("%d\n", 10 > 5);
printf("%d\n", 10 < 5);
Output:
1
0
int age = 25;
int limit = 18;
int result = age > limit;
printf("%d", result);
Output:
1
The result can be stored in an integer variable.
Relational operators are frequently used with if statements.
int age = 20;
if (age >= 18)
{
printf("Eligible");
}
The message is displayed because the condition is true.
int marks = 35;
if (marks >= 40)
{
printf("Pass");
}
else
{
printf("Fail");
}
Output:
Fail
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("Adult");
}
return 0;
}
The relational operator compares the entered age with 18.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
{
printf("A is greater");
}
else if (a < b)
{
printf("B is greater");
}
else
{
printf("Both are equal");
}
return 0;
}
This program uses three relational comparisons to compare two numbers.
Characters can also be compared because character constants have integer values based on the execution character set.
char a = 'A';
char b = 'B';
printf("%d", a < b);
The result is commonly 1 in character sets where
'A' has a smaller value than 'B', such as ASCII.
float price = 99.5;
if (price > 50.0)
{
printf("Price is greater than 50");
}
Relational operators can compare floating-point values as well.
A common beginner mistake is confusing assignment = with
equality comparison ==.
int age = 20;
if (age == 20)
{
printf("Age is 20");
}
Here, = assigns a value, while == compares values.
Relational expressions can be combined with logical operators to create more complex conditions.
int age = 25;
int marks = 70;
if (age >= 18 && marks >= 40)
{
printf("Condition satisfied");
}
Here, two relational expressions are combined using logical AND.
#include <stdio.h>
int main()
{
int a = 50;
int b = 30;
if (a > b)
{
printf("A is largest");
}
else
{
printf("B is largest");
}
return 0;
}
The > operator determines which number is larger.
#include <stdio.h>
int main()
{
int marks = 55;
if (marks >= 40)
{
printf("Pass");
}
else
{
printf("Fail");
}
return 0;
}
The >= operator checks whether the marks meet the minimum
required value.
int number = 50;
if (number >= 1 && number <= 100)
{
printf("Number is between 1 and 100");
}
The relational operators check both boundaries of the range.
= instead of == for comparison.> with >=.< with <=.0.1.#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Equal: %d\n", a == b);
printf("Not Equal: %d\n", a != b);
printf("Greater: %d\n", a > b);
printf("Less: %d\n", a < b);
printf("Greater or Equal: %d\n", a >= b);
printf("Less or Equal: %d\n", a <= b);
return 0;
}
This program demonstrates all six relational operators.
Practice the following programs:
Practice these comparisons with if and if-else
statements to become comfortable with decision-making in C.
== checks whether two values are equal.!= checks whether two values are different.> checks for greater than.< checks for less than.>= checks for greater than or equal to.<= checks for less than or equal to.1.0.if,
if-else, and loops.Question: Which operator is used to check whether two values are equal in C?