Table of contents
1.
Introduction 
2.
What are relational operators in C?
3.
Summary of Relational Operators in C
4.
Types of Relational Operators in C
4.1.
1. Less than Operator(<)
4.1.1.
Syntax  
4.1.2.
Code
4.2.
C
4.2.1.
Output 1
4.2.2.
Explanation 
4.2.3.
Output 2
4.2.4.
Explanation 
4.3.
2. Greater than Operator(>)
4.3.1.
Syntax 
4.3.2.
Code 
4.4.
C
4.4.1.
Output 1  
4.4.2.
Explanation 
4.4.3.
Output 2
4.4.4.
Explanation 
4.5.
3. Equal to Operator(==)
4.5.1.
Syntax 
4.5.2.
Code  
4.6.
C
4.6.1.
Output 1  
4.6.2.
Explanation  
4.6.3.
Output 2 
4.6.4.
Explanation  
4.7.
4. Not Equal to Operator(!=)
4.7.1.
Syntax 
4.7.2.
Code  
4.8.
C
4.8.1.
Output 1
4.8.2.
Explanation 
4.8.3.
Output 2
4.8.4.
Explanation 
4.9.
5. Less than equal to the Operator(<=)
4.9.1.
Syntax
4.9.2.
Code  
4.10.
C
4.10.1.
Output 1  
4.10.2.
Explanation  
4.10.3.
Output 2 
4.10.4.
Explanation  
4.11.
6. Greater than equal to Operator(>=)
4.11.1.
Syntax
4.11.2.
Code 
4.12.
C
4.12.1.
Output 1 
4.12.2.
Explanation 
4.12.3.
Output 2  
4.12.4.
Explanation  
5.
Frequently Asked Questions
5.1.
What are relational operators also known as?
5.2.
What is the relational operator's precedence in C?
5.3.
What is the output of relational operators?
5.4.
Is “=!” A relational operator in C?
6.
Conclusion
Last Updated: Aug 31, 2024
Easy

Relational Operators in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In programming, relational operators are crucial in comparing values and making decisions. Regarding the C language, relational operators allow you to compare variables and determine their relationships. Whether you're checking for equality, inequality, or other conditions, C relational operators provide a powerful tool for controlling the flow of your program. In this article, we will discuss different types of relational operator, their syntax, and examples.

Relational Operators in C

What are relational operators in C?

Relational operators are crucial components of C language, which enable developers to compare values and create logical expressions. These operators evaluate the relationship between two operands and return a boolean result of either true (1) or false (0) based on the specified condition. The relational operators in C include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). With the help of these operators, programmers can make informed decisions, control program flow, and implement conditional statements and loops that execute specific code blocks based on whether a condition is satisfied.

Summary of Relational Operators in C

OperatorDescriptionExample
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: 

  1. Less than Operator (<)
  2. Greater than Operator (>)
  3. Equal to Operator (==)
  4. Not Equal to Operator (!=)
  5. Less than equal to Operator (<=)
  6. Greater than equal to Operator (>=)

1. 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;
}
You can also try this code with Online C Compiler
Run Code

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”.

2. 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;
}
You can also try this code with Online C Compiler
Run Code

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

3. 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;
}
You can also try this code with Online C Compiler
Run Code

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

4. 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;
}
You can also try this code with Online C Compiler
Run Code

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”.

5. Less than equal to the 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;
}
You can also try this code with Online C Compiler
Run Code

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”.

6. 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;
}
You can also try this code with Online C Compiler
Run Code

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

What are relational operators also known as?

Relational operators are also known as comparison operators, as they compare the values of two operands.

What is the relational operator's precedence in C?

In C, the precedence of relational operators is lower than arithmetic operators but higher than logical operators.

What is the output of relational operators?

The output of relational operators is either 0 (false) or 1 (true), representing the result of the comparison.

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 Code 360 to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Happy Learning!

Live masterclass