• C Programming Video Tutorials

Relational Operators in C



The relational operators in C are the symbols that are defined to perform comparison of two values. The familiar angular brackets < and > are the relational operators in addition to few more as listed in a table below. These relational operators are used in a Boolean expression. All the relational operators evaluate to either true or false. C doesn’t have a Boolean data type. Instead, 0 is interpreted as false and any nonzero result is treated as true.

Here is a simple example of relational operator in C −

Example

#include <stdio.h>

int main() {
   int op1 = 5;
   int op2 = 3;

   printf("op1: %d op2: %d op1<op2: %d\n", op1, op2, op1<op2);

   return 0;
}

Output

op1: 5 op2: 3 op1<op2: 0

The relational operators have an important role to play in decision control and looping statements in C.

The following table lists all the relational operators in C −

Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B)
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B)
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B)
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B)
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B)
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B)

All the relational operators are binary operators. Since they perform comparison, they need two operands on their either side.

The = symbol is defined as the assignment operator in C. Hence, C uses == (double equal) as equality operator. The angular brackets > and < are defined as greater than and less than operators. When combined with = symbol, they form >= operator for greater than or equal, and <= operator for less than or equal comparison. Finally, the = symbol prefixed with !, i.e. != symbol is used as inequality operator.

Following example shows all the relational operators in use.

Example

#include <stdio.h>

int main() {

   int a = 21;
   int b = 10;
   int c ;
   
   printf("a: %d b: %d\n", a,b);
    
   if( a == b ) {
      printf("Line 1 - a is equal to b\n" );
   } else {
      printf("Line 1 - a is not equal to b\n" );
   }
	
   if ( a < b ) {
      printf("Line 2 - a is less than b\n" );
   } else {
      printf("Line 2 - a is not less than b\n" );
   }
	
   if ( a > b ) {
      printf("Line 3 - a is greater than b\n" );
   } else {
      printf("Line 3 - a is not greater than b\n" );
   }
   
   /* Lets change value of a and b */
   a = 5;
   b = 20;
   
   printf("a: %d b: %d\n", a,b);
	
   if ( a <= b ) {
      printf("Line 4 - a is either less than or equal to  b\n" );
   }
	
   if ( b >= a ) {
      printf("Line 5 - b is either greater than  or equal to b\n" );
   }
   
   if( a != b ) {
      printf("Line 6 - a is not equal to b\n" );
   } else {
      printf("Line 6 - a is equal to b\n" );
   }
   return 0;
}

Output

a: 21 b: 10
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
a: 5 b: 20
Line 4 - a is either less than or equal to  b
Line 5 - b is either greater than  or equal to b
Line 6 - a is not equal to b

The == operator needs to be used with care. Remember that = (single equal) is the assignment operator in C. If used by mistake in place of equality operator, you get incorrect output as follows −

Example

#include <stdio.h>

int main() {
   int a = 5;
   int b = 3;

   if (a=b)
   {
      printf("a is equal to b");
   }
   else
   {
      printf("a is not equal to b");
   }
   return 0;
}

Output

a is equal to b

In fact, the value of b is assigned to a here, which is non-zero, and hence the if expression becomes true.

The char types can also be used as the operand for all the relational operators, as a char type is a subset of int type.

Example

#include <stdio.h>

int main() {

   char a = 'B';
   char b = 'd';

   printf("a: %c b: %c\n", a,b);

   if( a == b ) {
      printf("Line 1 - a is equal to b\n" );
   } else {
      printf("Line 1 - a is not equal to b\n" );
   }

   if ( a < b ) {
      printf("Line 2 - a is less than b\n" );
   } else {
      printf("Line 2 - a is not less than b\n" );
   }

   if ( a > b ) {
      printf("Line 3 - a is greater than b\n" );
   } else {
      printf("Line 3 - a is not greater than b\n" );
   }
   if( a != b ) {
      printf("Line 4 - a is not equal to b\n" );
   } else {
      printf("Line 4 - a is equal to b\n" );
   }
   return 0;
}

Output

a: B b: d
Line 1 - a is not equal to b
Line 2 - a is less than b
Line 3 - a is not greater than b
Line 4 - a is not equal to b

However, the relational operators cannot be used for comparing secondary types such as array, or derived types such as struct or union types.

c_operators.htm
Advertisements