Updated March 23, 2023
Introduction to Relational Operators in C
Relational operators are those which are part of Binary operators. These operators are generally used in the comparison between two values or conditions. This comparison condition drives us to the Boolean expression values by which the code written is accordingly executed.
Types of Relational Operators in C Language
Below are the different types of relational operators in C language:
1. Greater than (>)
This operator checks and executes the code according to the ‘greater than’ functionality. It checks if the left side operands or the right side operands are greater and executes the condition that way.
Code:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a>b)
{
printf(" a’s value is greater than b’s value");
}
else
{
printf(" b’s value is greater than a’s value");
}
}
Output:
The program compares if the value of ‘a’ is greater or if the value of ‘b’ is greater and executes if the condition to give us the output.
2. Less than (<)
This operator is similar to that of greater than the operator that is discussed above. The only difference is the output is expected to give us the operand whose value is less than that of the other one.
Output:
The same example values as of above are used to make a better understanding on how these greater- than and less-than operators work according to the conditions.
3. Greater than and equal to (>=)
This operator not only satisfies the ‘greater than’ condition but also includes if the left side operand is equal to the right side operand or not.
Code:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a>b)
{
printf("Value of a is greater than b");
}
else if(a>=b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of b is greater than a");
}
}
Output:
Here, we have used both the conditions and gave an example for the values that are equal to each other. So, we have got equal conditions executed.
4. Less than and equal to (<)
This operator is similar to the above mentioned ‘greater than and equal to’ operator. The only difference is with the operators ‘less than’ and ‘greater than’.
Code:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a<b)
{
printf("Value of a is less than b");
}
else if(a<=b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of b is less than a");
}
}
Output:
As we are having equal conditions in priority with ‘less than’ operator, we had given the example accordingly.
5. Double equal to (==)
This operator only defines if the left side operand is equal to the right side operand. This also means that it is not going to consider the conditions ‘greater than’ or ‘less than’ but only the equal condition.
Code:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a==b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of a is not equal to b");
}
}
Output:
For comparing the equal condition between left and right operand values, we should use the ‘double equal to’ operator for it.
6. Not Equal to (!=)
This operator is for doing the opposite functionality of that mentioned above. This is for making the condition between the left and right operands should not be equal to each other.
Code:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a!=b)
{
printf("Value of a is not equal to b");
}
else
{
printf("Value of a is equal to b");
}
}
Output:
Below given is a random example used by utilizing all the relational operators:
#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if((a-b) > b)
{
printf(" Subtracted value of a and b is greater than b");
}
else if((a-b) < b)
{
printf(" Subtracted value of a and b is less than b");
}
else if((a==b))
{
printf(" Value of a and b are equal to each other");
}
else if((a+5)!=(b+6))
{
printf(" Value of sum of a and 5 is not equal to sum of b and 6");
}
else if((a*b) >= (b*b))
{
printf(" Multiplied value of a and b is greater than or equal tovalue of square of b");
}
else if((a*b) <= (b*b))
{
printf(" Multiplied value of a and b is less than or equal to value of square of b");
}
else
{
printf(" Tried all the relational operators");
}
}
Output:
Conclusion
So, this is how we can use different relational operators. These operations play a very important role in any programming language. And here we had done the same in C programming language.
Recommended Articles
This has been a guide to Relational Operators in C. Here we discuss the introduction and different types of relational operators in C. you may also have a look at the following articles to learn more –