C Programming
In this tutorial we will learn about relational operators in C programming language.
C provides us with 6 relational operators.
Operator | Description |
---|---|
< | Is less than |
<= | Is less than or equal to |
> | Is greater than |
>= | Is greater than or equal to |
== | Is equal to |
!= | Is not equal to |
We use relational operators to compare two quantities. If the condition is satisfied then we get one (true) otherwise, it is zero (false).
In the following example we have two numbers and we are checking if a is less than b.
#include <stdio.h>
int main(void)
{
int
a = 10,
b = 20;
if (a < b) {
printf("a is less than b\n");
}
else {
printf("a is not less than b\n");
}
return 0;
}
Output
a is less than b
Note! In the above program and in the programs that follows we are using if-else
conditional statement. If the condition is true or non-zero then the if-block code is executed. Otherwise, the else-block code is executed.
We will learn more about if-else in the later tutorial.
In the following example we have two numbers and we are checking if a is less than or equal to b.
#include <stdio.h>
int main(void)
{
int
a = 10,
b = 20;
if (a <= b) {
printf("a is less than or equal to b\n");
}
else {
printf("a is not less than and not equal to b\n");
}
return 0;
}
Output
a is less than or equal to b
In the following example we have two numbers and we are checking if a is greater than b.
#include <stdio.h>
int main(void)
{
int
a = 20,
b = 10;
if (a > b) {
printf("a is greater than b\n");
}
else {
printf("a is not greater than b\n");
}
return 0;
}
Output
a is greater than b
In the following example we have two numbers and we are checking if a is greater than or equal to b.
#include <stdio.h>
int main(void)
{
int
a = 20,
b = 10;
if (a >= b) {
printf("a is greater than or equal to b\n");
}
else {
printf("a is not greater than and not equal to b\n");
}
return 0;
}
Output
a is greater than or equal to b
In the following example we have two numbers and we are checking if a is equal to b.
#include <stdio.h>
int main(void)
{
int
a = 10,
b = 10;
if (a == b) {
printf("a is equal to b\n");
}
else {
printf("a is not equal to b\n");
}
return 0;
}
Output
a is equal to b
In the following example we have two numbers and we are checking if a is not equal to b.
#include <stdio.h>
int main(void)
{
int
a = 20,
b = 10;
if (a != b) {
printf("a is not equal to b\n");
}
else {
printf("a is equal to b\n");
}
return 0;
}
Output
a is not equal to b
Among the six relational operator, each one is a complement of another operator.
Operator | Is Complement of |
---|---|
> | <= |
< | >= |
== | != |
ADVERTISEMENT