Sunday, 5 May 2019

Relational Operators in C


Relational operators are used to compare two values. There are six relational operators in C. These are called relational operator because they establish relation between its operands. These operators are used in decision-making. These are binary operators because they need two operands.
Following table lists the six relational operators and their meaning-
Sl No
Operator
Syntax
Meaning
1
< 
Op1<Op2
Is Op1 less than Op2
2
<=
Op1<=Op2
Is Op1 less than or equal to Op2
3
> 
Op1>Op2
Is Op1 greater than Op2
4
>=
Op1>=Op2
Is Op1 greater than or equal to  Op2
5
==
Op1==Op2
Is Op1 equal to Op2
6
!=
Op1!=Op2
Is Op1 not equal to than Op2

Each of these operations results in a value of type int. The result of each operation is 1 if the comparison is true and 0 if the comparison is false.

Following C program demonstrates the working of above relational operators-
#include<stdio.h>
int main(){
   int a=1,b=2;
   if(a<b){
      printf(“a is less than b.\n”);
    }

   a=3,b=2;
   if(a<=b){
      printf(“a is less than or equal to b.\n”);
    }
  a=2,b=1;
   if(a>b){
      printf(“a is grater than b.\n”);
    }

  a=2,b=3;
   if(a>=b){
     printf(“a is greater than or equal to b.\n”);
    }

a=4,b=4;
if(a==b){
     printf(“a is equal to b.\n”);
    }
if(a!=b){
     printf(“a is not equal to b.\n”);
    }

  return 0;
}
Output:
a is less than b.
a is grater than b.
a is equal to b.

Please comment and share if you find it useful.

No comments:

Post a Comment