Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

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.

In this article, we will take a look into the Relational Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Types of Relational Operators in C

The relational operators are used to compare two of the available values to understand what relationship the pairs of values share. For example, equal to, greater than, less than, etc. Here is a list of all the relational operators used in the C language:

  • Equal to
  • Not equal to
  • Less than
  • Greater than
  • Less than or equal to
  • Greater than or equal to

Working of Relational Operators in C

Let us take a look at each of these in more detail.

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.

Not equal to Operator

The function of the not equal to operator (!=) is to check if two of the available operands are equal to each other or not. If it is not equal, then it returns to be true, or else it returns false. For instance, 3!=3 will return to be false. On the other hand, 3!=4 will return to be true.

Less than Operator

The function of the less than operator (<) is to check if the first available operand is lesser than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 5<4 will return to be false. On the other hand, 6<8 will return to be true.

Greater than Operator

The function of the greater than operator (>) is to check if the first available operand is greater than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 5>4 will return to be true. On the other hand, 7>8 will return to be false.

Less than or equal to Operator

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. If it is so, then it returns to be true, or else it returns false. For instance, 9<=9 and 6<=9 will return to be true. On the other hand, 9<=8 will return to be false.

Greater than or equal to operator

The function of the greater than or equal to operator (>=) is to check if the first available operand is equal to or greater than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 2>=2 and 4>=2 will return to be true. On the other hand, 3>=4 will return to be false.

Summary of Working of Relational Operators in C

Here is a table that discusses, in brief, all the Relational operators that the C language supports. If P=5 and Q=3, then:

Meaning Operator Details Example
Equal to == (P == Q) is not true 4 == 5 gets evaluated to 0

4 == 4 gets evaluated to 1

Not equal to != (P != Q) is true 4 != 5 gets evaluated to 1

4 != 4 gets evaluated to 0

Less than < (P < Q) is not true 4 < 2 gets evaluated to 0

4 < 6 gets evaluated to 1

Greater than > (P > Q) is true 4 > 2 gets evaluated to 1

4 > 9 gets evaluated to 0

Less than or equal to <= (P <= Q) is not true 4 <= 2 gets evaluated to 0

4 <= 6 gets evaluated to 1

4 <= 4 gets evaluated to 1

Greater than or equal to >= (P >=Q) is true 4 >= 2 It gets evaluated to 1

4 >= 9 It gets evaluated to 0

4 <= 4 gets evaluated to 1

Here, 0 means false, and 1 means true.

Example of Relational Operators in C

Let us look at an example to understand how these work in a code:

#include <stdio.h>

int main()

{

int p = 5, q = 5, r = 10;

printf(“%d == %d is %d \n”, p, r, p == r);

printf(“%d == %d is %d \n”, p, q, p == q);

printf(“%d > %d is %d \n”, p, r, p > r);

printf(“%d > %d is %d \n”, p, q, p > q);

printf(“%d < %d is %d \n”, p, r, p < r);

printf(“%d < %d is %d \n”, p, q, p < q);

printf(“%d != %d is %d \n”, p, r, p != r);

printf(“%d != %d is %d \n”, p, q, p != q);

printf(“%d >= %d is %d \n”, p, r, p >= r);

printf(“%d >= %d is %d \n”, p, q, p >= q);

printf(“%d <= %d is %d \n”, p, r, p <= r);

printf(“%d <= %d is %d \n”, p, q, p <= q);

return 0;

}

The output generated here would be:

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

Let us look at another example of a relational operator in C with integers:

#include <stdio.h>

int main()

{

int p = 9;

int b = 4;

printf(“p > q: %d \n”, p > q);

printf(“p >= q: %d \n”, p >= q);

printf(“p <= q: %d \n”, p <= q);

printf(“p < q: %d \n”, p < q);

printf(“p == q: %d \n”, p == q);

printf(“p != q: %d \n”, p != q);

}

The output generated here would be:

p > q: 1

p >= q: 1

p <= q: 0

p < q: 0

p == q: 0

p != q: 1

Practice Problems on Relational Operators in C

1. What would be the output obtained from the program given below?

#include<stdio.h>

int main()

{

int p, q, r;

p = 8 > 5 > 2;

q = 8 > 5 > 0;

r = 8 > 5 > 1;

printf(“%d %d %d”, p, q, r);

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

9 is not equal to 10. Thus, p is not equal to q.

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

Q1

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.

Q2

Does the equal to operator work with a single = sign?

No, it doesn’t. You must use == double equal to signs for the equal to operator to work.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*