Relational Operators in C
Relational Operators in C
Relational Operators in C
These types of operators check the relationship between two of the available operands. In case
the relation happens to be true, then it returns to 1. But in case this relation turns out to be false,
then it returns to the value 0. We use the relational operators in loops and in the cases of
decision making.
Table of Contents
● Equal to
● Not equal to
● Less than
● Greater than
● Less than or equal to
● Greater than or equal to
Equal to Operator
The function of the equal to operator (==) is to check if two of the available operands are equal to
each other or not. If it is so, then it returns to be true, or else it returns false. For instance, 3==3
will return to be true. On the other hand, 2==3 will return to be false.
#include <stdio.h>
int main()
int p = 5, q = 5, r = 10;
printf(“%d == %d is %d \n”, p, r, p == r);
return 0;
5 == 10 is 0
5 == 5 is 1
5 > 10 is 0
5 > 5 is 0
5 < 10 is 1
5 < 5 is 0
5 != 10 is 1
5 != 5 is 0
5 >= 10 is 0
5 >= 5 is 1
5 <= 10 is 1
5 <= 5 is 1
#include <stdio.h>
int main()
{
int p = 9;
int b = 4;
p > q: 1
p >= q: 1
p <= q: 0
p < q: 0
p == q: 0
p != q: 1
#include<stdio.h>
int main()
int p, q, r;
p = 8 > 5 > 2;
q = 8 > 5 > 0;
r = 8 > 5 > 1;
return 0;
A. 0 1 1
B. 1 0 0
C. 0 1 0
D. 0 0 1
Answer – C. 0 1 0
The execution for > occurs from left to right. Thus, in the statement p = 8 > 5 > 2; first 8 > 5 will be
evaluated, and thus the result will be 1. Now, p = 1 > 2; thus, the result obtained here will be 0.
Using a similar kind of calculation, we will get results for q and r to be 1 and 0, respectively.
2. What would be the output obtained from the program given below?
#include< stdio.h>
int main()
int p, q, r;
p = 9;
q = 10;
r = p == q;
printf(“%d”, c);
return 0;
A. 1
B. 0
C. Garbage Value
D. None of these
Answer – B. 0
3. What would be the output obtained from the program given below?
#include<stdio.h>
int main()
int p, q, r, s;
p = 6 < 5 < 8;
q = 6 < 5 < 0;
r = 6 < 5 < 1;
s = 6 < 7 > 1;
printf(“%d %d %d %d”, p, q, r, s);
return 0;
A. 0 0 1 1
B. 1 0 0 1
C. 0 1 1 0
D. 1 1 0 0
Answer – D. 1 1 0 0
The execution for < occurs from left to right. Thus, in the statement p = 6 < 5 < 8; first 6 < 5 will be
evaluated, and thus the result will be 0. Now, p = 0 < 8; thus, the result obtained here will be 1.
Using a similar kind of calculation, we will get results for q, r, and s to be 1, 1, and 0, respectively.
FAQs
What is the difference between the greater than/ less than operator
and the greater than and equal to/ less than or equal to operator?
The function of the less than operator (<) is to check if the first available operand is lesser than
the second one. On the other hand, the function of the less than or equal to operator (<=) is to
check if the first available operand is equal to or less than the second one. The same comparison
goes for the greater than operator and the greater than or equal to operator.
For example, 5 < 6 will be true and 5 < 5 will be false. But both 5 <= 6 and 5 <= 5 will be true. Here,
both 5 < 4 and 5 <= 4 will be false.
Similarly, 7 > 2 will be true and 7 > 7 will be false. But both 7 >= 2 and 7 >= 7 will be true. Here,
both 7 > 9 and 7 >= 9 will be false.
No, it doesn’t. You must use == double equal to signs for the equal to operator to work.