Relational operators are binary operators(operates on two operands) and are used to relate or compare two operands. There are four relational operators in C (i.e
<, <=, >, >=
). If the relationship between the operands is correct, it will return 1 and returns 0 otherwise.
Apart from four relational operators, C has two equality operator (== and !=) as well for comparing operands. Now let's take a look at different relational and equality operators and how they operate on the operands.
Operator | Name | Description | Example |
---|---|---|---|
< | 'Less than' operator | Checks if the first operand is less than second operand and returns 1 if it's true, else returns 0 | 10 < 5 returns 0 and5 < 10 returns 1 |
<= | 'Less than or equals to' operator | Checks if the first operand is less than or equals to second operand and returns 1 if it's true, else returns 0 | 10 <= 10 returns 1 and 10 <= 5 returns 0 |
> | 'Greater than' operator | Checks if the first operand is greater than second operand and returns 1 if it's true, else returns 0 | 10 > 5 returns 1 and5 > 10 returns 0 |
>= | 'Greater than or equals to' operator | Checks if the first operand is greater than or equals to second operand and returns 1 if it's true, else returns 0 | 10 >= 10 returns 1 and 5 >= 10 returns 0 |
== | Equality operator | Checks if the two operands are equal are returns 1 if it's true, else returns 0 | 10 == 10 returns 1 and 10 == 1 returns 0 |
!= | Non-equality operator | Checks if the two operands are equal are returns 1 if they are not equal, else returns 0 | 10 != 10 returns 0 and 10 != 1 returns 1 |
Examples of Relational Operator
- #include <stdio.h>
- int main()
- {
- int a = 25, b = 10;
- printf("%d < %d : %d\n", a, b, a < b);
- printf("%d <= %d: %d\n", a, b, a <= b);
- printf("%d > %d : %d\n", a, b, a > b);
- printf("%d >= %d: %d\n", a, b, a >= b);
- printf("%d == %d: %d\n", a, b, a == b);
- printf("%d != %d: %d\n", a, b, a != b);
- return 0;
- }
- Output:
- 25 < 10 : 0
- 25 <= 10: 0
- 25 > 10 : 1
- 25 >= 10: 1
- 25 == 10: 0
- 25 != 10: 1
No comments:
Post a Comment