Do you think IIT Guwahati certified course can help you in your career?
No
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.
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
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 (<)
Greater than Operator (>)
Equal to Operator (==)
Not Equal to Operator (!=)
Less than equal to Operator (<=)
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); }
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
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; }
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
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”.
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); }
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
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”.
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); }
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
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); }
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
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); }
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
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”.
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: