Sunday, January 31, 2016

Relational operator

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.
OperatorNameDescriptionExample
<'Less than' operatorChecks if the first operand is less than second operand and returns 1 if it's true, else returns 010 < 5 returns 0 and5 < 10 returns 1
<='Less than or equals to' operatorChecks if the first operand is less than or equals to second operand and returns 1 if it's true, else returns 010 <= 10 returns 1 and 10 <= 5 returns 0
>'Greater than' operatorChecks if the first operand is greater than second operand and returns 1 if it's true, else returns 010 > 5 returns 1 and5 > 10 returns 0
>='Greater than or equals to' operatorChecks if the first operand is greater than or equals to second operand and returns 1 if it's true, else returns 010 >= 10 returns 1 and 5 >= 10 returns 0
==Equality operatorChecks if the two operands are equal are returns 1 if it's true, else returns 010 == 10 returns 1 and 10 == 1 returns 0
!=Non-equality operatorChecks if the two operands are equal are returns 1 if they are not equal, else returns 010 != 10 returns 0 and 10 != 1 returns 1

Examples of Relational Operator

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a = 25, b = 10;
  6. printf("%d < %d : %d\n", a, b, a < b);
  7. printf("%d <= %d: %d\n", a, b, a <= b);
  8. printf("%d > %d : %d\n", a, b, a > b);
  9. printf("%d >= %d: %d\n", a, b, a >= b);
  10. printf("%d == %d: %d\n", a, b, a == b);
  11. printf("%d != %d: %d\n", a, b, a != b);
  12. return 0;
  13. }
  14.  
  15. Output:
  16. 25 < 10 : 0
  17. 25 <= 10: 0
  18. 25 > 10 : 1
  19. 25 >= 10: 1
  20. 25 == 10: 0
  21. 25 != 10: 1
  22.  

No comments: