The relational operators (or comparison operators) are used to check the relationship between two operands. It checks whether two operands are equal or not equal or less than or greater than, etc.
It returns 1 if the relationship checks pass, otherwise, it returns 0.
For example, if we have two numbers 14 and 7, if we say 14 is greater than 7, this is true, hence this check will return 1 as the result with relationship operators. On the other hand, if we say 14 is less than 7, this is false, hence it will return 0.
The following table shows all relational operators supported in the C language.
Operator | Description | Example(a and b , where a = 10 and b = 11) |
---|---|---|
== | Check if two operands are equal | a == b , returns 0 |
!= | Check if two operands are not equal. | a != b , returns 1 because a is not equal to b |
> | Check if the operand on the left is greater than the operand on the right | a > b , returns 0 |
< | Check operand on the left is smaller than the right operand | a < b , returns 1 |
>= | check left operand is greater than or equal to the right operand | a >= b , returns 0 |
<= | Check if the operand on left is smaller than or equal to the right operand | a <= b , returns 1 |
Example: Relational Operators
When we use relational operators, then based on the result of the comparison done, if it’s true, then the output is 1 and if it’s false, then the output is 0. We will see the same in the example below.
#include <stdio.h>
int main() {
int a = 10, b = 20, result;
// Equal
result = (a==b);
printf("(a == b) = %d \n",result);
// less than
result = (a<b);
printf("(a < b) = %d \n",result);
// greater than
result = (a>b);
printf("(a > b) = %d \n",result);
// less than equal to
result = (a<=b);
printf("(a <= b) = %d \n",result);
return 0;
}
Output
(a == b) = 0 (a < b) = 1 (a > b) = 0 (a <= b) = 1
In the code example above, a
has value 10, and b
has value 20, and then different comparisons are done between them.
In the C language, true is any value other than zero. And false is zero.