Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
What are Operators? 
3.
What are relational operators in C?
4.
Summary of Relational Operators in C
5.
Types of Relational Operators in C
5.1.
Less than Operator
5.1.1.
Syntax  
5.1.2.
Code
5.2.
C
5.2.1.
Output 1
5.2.2.
Explanation 
5.2.3.
Output 2
5.2.4.
Explanation 
5.3.
Greater than Operator
5.3.1.
Syntax 
5.3.2.
Code 
5.4.
C
5.4.1.
Output 1  
5.4.2.
Explanation 
5.4.3.
Output 2
5.4.4.
Explanation 
5.5.
Equal to Operator 
5.5.1.
Syntax 
5.5.2.
Code  
5.6.
C
5.6.1.
Output 1  
5.6.2.
Explanation  
5.6.3.
Output 2 
5.6.4.
Explanation  
5.7.
Not Equal to Operator
5.7.1.
Syntax 
5.7.2.
Code  
5.8.
C
5.8.1.
Output 1
5.8.2.
Explanation 
5.8.3.
Output 2
5.8.4.
Explanation 
5.9.
Less than equal to Operator 
5.9.1.
Syntax
5.9.2.
Code  
5.10.
C
5.10.1.
Output 1  
5.10.2.
Explanation  
5.10.3.
Output 2 
5.10.4.
Explanation  
5.11.
Greater than equal to Operator 
5.11.1.
Syntax
5.11.2.
Code 
5.12.
C
5.12.1.
Output 1 
5.12.2.
Explanation 
5.12.3.
Output 2  
5.12.4.
Explanation  
6.
Frequently Asked Questions
6.1.
Which is not a relational operator in C?
6.2.
What are logical operators in C?
6.3.
Can relational operators be used to compare two strings in C?
6.4.
Can relational operators be used to compare any data types in C?
6.5.
How do relational operators work in C?
6.6.
Is “=!” A relational operator in C?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Relational Operators in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

Hello Ninjas! Just like how we compare numbers using special symbols in mathematics, the language C also provides us with special operators that can be used to compare any two values. So today, we are going to learn about different relational operators in C and will try to code the same. Let’s learn!

Relational Operators in C

Let’s start from the basics and learn about operators.

What are Operators? 

Operators are ones that perform an operation on given operands. These are the special symbols that perform operations on operands.

The operators in C are categorized as - 

  1. Arithmetic Operators
  2. Relational Operators
  3. Assignment Operators
  4. Logical Operators
  5. Identity Operators
  6. Bitwise Operators
  7. SizeOf Operators
  8. Conditional Operators
  9. Comma Operators
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What are relational operators in C?

Relational Operators are those operators that are used to perform comparisons among operands. It returns a true or a false based on the given condition. These are essential parts of any programming language. Through relational operators, we can create logical expressions and make decisions based on the boolean results. These help in the control flow and implementation of conditions in programs.

Summary of Relational Operators in C

Operator Description Example
Less Than Operator (<) Compares the two given values and returns true if the operand at left is smaller than right. x<y
Greater Than Operator (>) Compares the two given values and returns true if the operand at left is greater than right. x>y
Equal To Operator(==) Checks if both operands have equal value. x==y
Not Equal To Operator(!=) Checks if both operands have unequal value. x!=y
Less Than Equal To Operator(<=) Checks if the value of the operand on the left is less than or equal to right. x<=y
Greater Than Equal To Operator(>=) Checks if the value of the operand on the left is greater than or equal to right. x>=y

Types of Relational Operators in C

The various kinds of relational operators in C are: 

Less than Operator

The less than Operator is represented as ‘<’. It is used to compare the two given values and returns a true if the operand at left is smaller than the operand at right.

Syntax  

The syntax of less than operator for 2 operands, say, P and Q, is:

P < Q;

Code

  • C

C

#include <stdio.h>
#include <stdbool.h>

int main() {
int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using less than (<) operator
storing the answer in variable 'result' */
result = operand1 < operand2;

// if result is true that implies operand1 is smaller than operand2
if (result == true) {
printf("%d is less than %d", operand1, operand2);
}
// if result is false that implies operand2 is smaller than operand1
else {
printf("%d is not less than %d", operand1, operand2);
}

return 0;
}

Output 1

Output1 of less than operator

Explanation 

Here operand1 contains the value 53, and operand2 contains 87. As 53 is less than 87, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “53 is less than 87”.

Output 2

Output2 of less than operator

Explanation 

Here operand1 contains the value 93, and operand2 contains 44. As 93 is not less than 44, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “93 is not less than 44”.

Greater than Operator

The greater than operator is represented as ‘>’. It compares the two given values and returns a true if the operand at left is greater than the operand at right.

Syntax 

The syntax of greater than operator for 2 operands, say, P and Q, is:

P > Q;

Code 

  • C

C

#include <stdio.h>
#include <stdbool.h>
int main() {

int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using greater than (>) operator
storing the answer in variable 'result' */
result = operand1 > operand2;

// if result is true that implies operand1 is greater than operand2
if (result == true) {
printf("%d is greater than %d", operand1, operand2);
}
// if result is false that implies operand2 is greater than operand1
else {
printf("%d is not greater than %d", operand1, operand2);
}
return 0;
}

Output 1  

Output1 of greater than operator

Explanation 

Here operand1 contains 98, and operand2 contains 29. As 98 is greater than 29, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “98 is greater than 29”.

Output 2

Output2 of greater than operator

Explanation 

Here operand1 contains 9, and operand2 contains 33. As 9 is not greater than 33, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “9 is not greater than 33”.

Also see, Floyd's Triangle in C

Equal to Operator 

The equal to Operator is represented as ‘==’. It is used to check if both operands have equal value. If they contain equal values, it returns true; else, false.

Syntax 

The syntax of equal to operator for 2 operands, say, P and Q, is:

P == Q;

Code  

  • C

C

#include <stdio.h>
#include <stdbool.h>
int main() {

int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using equal to (=) operator
storing the answer in variable 'result' */
result = operand1 == operand2;

// if result is true that implies operand1 is equal to operand2
if (result == true) {
printf("%d is equal to %d", operand1, operand2);
}
// if result is false that implies operand1 is not equal to operand2.
else {
printf("%d is not equal to %d", operand1, operand2);
}

return 0;
}

Output 1  

Output1 of equal to operator

Explanation  

Here operand1 contains 17, and operand2 contains 17. As both are equal, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “17 is equal to 17”.

Output 2 

Output2 of equal to operator

Explanation  

Here operand1 contains 13, and operand2 contains 15. As both are not equal, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “13 is not equal to 15”.

Must Read Passing Arrays to Function in C

Not Equal to Operator

The not equal to Operator is represented as ‘!=’. It is used to check if both operands have different values. If they contain different values, it returns true else, false.

Syntax 

The syntax of not equal to operator for 2 operands, say, P and Q, is:

P != Q;

Code  

  • C

C

#include <stdio.h>
#include <stdbool.h>
int main() {
int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using not equal to (!=) operator
storing the answer in variable 'result' */
result = operand1 != operand2;

// if result is true that implies operand1 is not equal to operand2
if (result == true) {
printf("%d is not equal to %d", operand1, operand2);
}
// if result is false that implies operand1 is equal to operand2.
else {
printf("%d is equal to %d", operand1, operand2);
}

return 0;
}

Output 1

Output1 of not equal to operator

Explanation 

Here operand1 contains 12, and operand2 contains 14. As both are not equal, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “12 is not equal to 14”.

Output 2

Output2 of not equal to operator

Explanation 

Here operand1 contains 64, and operand2 contains 64. As both are equal, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “64 is equal to 64”.

Less than equal to Operator 

Less than equal to Operator is represented as ‘<=’. It checks whether the value of the operand on the left is less than or equal to the operand on the right and returns true or false depending upon the operand's value.

Syntax

The syntax of less than equal to operator for 2 operands, say, P and Q, is:

P <= Q;

Code  

  • C

C

#include <stdio.h>
#include <stdbool.h>
int main() {

int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using less than equal to (<=) operator
storing the answer in variable 'result' */
result = operand1 <= operand2;

// if result is true that implies operand1 is less than or equal to operand2
if (result == true) {
printf("%d is less than or equal to %d", operand1, operand2);
}
// if the result is false that implies operand1 is not less than or equal operand2.
else {
printf("%d is not less than or equal to %d", operand1, operand2);
}

return 0;
}

Output 1  

output1 of less than or equal to operator

Explanation  

Here operand1 contains 45, and operand2 contains 88. As 45 is less than 88, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “45 is less than or equal to 88”.

Output 2 

output2 of less than or equal to operator

Explanation  

Here operand1 contains 67, and operand2 contains 65. As 67 is neither less than 65 nor equal to 65, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “67 is not less than or equal to 65”.

Greater than equal to Operator 

Greater than equal to Operator is represented as ‘>=’. It checks whether the value of the operand on the left is greater than or equal to the operand on the right and returns true or false depending upon the operand’s value.

Syntax

The syntax of greater than equal to operator for 2 operands, say, P and Q, is:

P >= Q;

Code 

  • C

C

#include <stdio.h>
#include <stdbool.h>
int main() {

int operand1, operand2;
bool result;

// taking input value for operand1
printf("Enter the value of numbers you want to compare :\n");
printf("Enter the value of first operand :");
scanf("%d/n", &operand1);

// taking input value for operand2
printf("Enter the value of second operand :");
scanf("%d", &operand2);

/* comparing the two operands using greater than equal to (>=) operator
storing the answer in variable 'result' */
result = operand1 >= operand2;

// if result is true that implies operand1 is greater than or equal to operand2
if (result == true) {
printf("%d is greater than or equal to %d", operand1, operand2);
}
// if result is false that implies operand1 is not greater than or equal operand2.
else {
printf("%d is not greater than or equal to %d", operand1, operand2);
}

return 0;
}

Output 1 

output 1 of greater than equal to operator

Explanation 

Here operand1 contains 40, and operand2 contains 2. As 40 is greater than 2, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “40 is greater than or equal to 2”.

Output 2  

Output2 of greater than equal to operator

Explanation  

Here operand1 contains -2, and operand2 contains 6. As -2 is neither greater than 6 nor equal to 6, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “-2 is not greater than or equal to 6”.

Also see, Tribonacci Series and  Short int in C Programming

Must Read Decision Making in C

Frequently Asked Questions

Which is not a relational operator in C?

In C programming, the "!" (exclamation mark) is not a relational operator. Instead, it is used as a logical NOT operator to reverse the truth value of an expression.

What are logical operators in C?

Logical operators in C are symbols used to manipulate Boolean values. They include && (logical AND), || (logical OR), and ! (logical NOT).

Can relational operators be used to compare two strings in C?

Though relational operators can be used to compare individual characters of a string based on their ASCII value, they can’t be used to compare two strings in C.

Can relational operators be used to compare any data types in C?

Yes, they can be used with any data type (int, float, char, etc) that supports comparison.

How do relational operators work in C?

Relational Operators in C work by comparing two operands and return a Boolean value (either true or false) based on the given condition.

Is “=!” A relational operator in C?

No, “=!” Is not a valid relational operator in C or in any other programming language. The correct representation is “!=”. 

Conclusion

In this article, we learned about different types of relational operators in C. We got to know that relational operators are an important part of programming. Further, we understood how these operators work and how to use them in code. We learned to write effective programs that perform the necessary operations and make logical decisions based on given conditions.

To learn more about Relational Operators, we recommend reading the following articles:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Learning!

Next article
Arithmetic Operators in C
Live masterclass