Relational Operators in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   10251 Share
☰ Table of Contents

Table of Content:


  • Relational Operators are used to checking relation between two variables or numbers.
  • Relational Operators are Binary Operators.
  • Relational Operators returns “Boolean” value .i.e it will return true or false.
  • Most of the relational operators are used in “If statement” and inside Looping statement in order to check truthiness or falseness of condition.

The following table shows all the relational operators supported by C language.

Operators Descriptions Examples
== (equal to) This operator checks the value of two operands, if both are equal, then it returns true otherwise false. (2 == 3) is not true.
!= (not equal to) This operator checks the value of two operands, if both are not equal, then it returns true otherwise false. (4 != 5) is true.
> (greater than) This operator checks the value of two operands, if the left side of the operator is greater, then it returns true otherwise false. (5 > 56) is not true.
< (less than) This operator checks the value of two operands if the left side of the operator is less, then it returns true otherwise false. (2 < 5) is true.
>= (greater than or equal to) This operator checks the value of two operands if the left side of the operator is greater or equal, then it returns true otherwise false. (12 >= 45) is not true.
<= (less than or equal to) This operator checks the value of two operands if the left side of the operator is less or equal, then it returns true otherwise false. (43 <= 43) is true.

 Examples of Arithmatic Operator

Program

#include
void main() {
	  int p = 5;
      int q = 10;

      printf("p == q = %d \n",(p == q) );
      printf("p != q = %d \n",(p != q) );
      printf("p > q = %d \n",(p > q) );
      printf("p < q = %d \n",(p < q) );
      printf("q >= p = %d \n",(q >= p) );
      printf("q <= p = %d \n",(q <= p) );
 }

Output

p == q = 0
p != q = 1
p > q = 0
p < q = 1
q >= p = 1
q <= p = 0
Press any key to continue . . .

Another Program

#include
void main() {
	  int a = 121;
   int b = 110;
   int c ;

   if( a == b ) {
      printf(" a is equal to b\n" );
   }
   else {
      printf(" a is not equal to b\n" );
   }
	
   if ( a < b ) {
      printf(" a is less than b\n" );
   }
   else {
      printf("  a is not less than b\n" );
   }
	
   if ( a > b ) {
      printf(" a is greater than b\n" );
   }
   else {
      printf(" a is not greater than b\n" );
   }
   
   /* Lets change value of a and b */
   a = 51;
   b = 120;
	
   if ( a <= b ) {
      printf(" a is either less than or equal to  b\n" );
   }
	
   if ( b >= a ) {
      printf(" b is either greater than  or equal to b\n" );
   }
 }

Output

 a is not equal to b
  a is not less than b
 a is greater than b
 a is either less than or equal to  b
 b is either greater than  or equal to b
Press any key to continue . . .