Relational Operators in C

The Relational operators are some of the Operators, which are mostly used either in If Conditions or Loops. Relational operators in C are commonly used to check the relationship between the two variables. If the relation is true, then it will return value 1. Otherwise, it returns value 0.

The below table shows all the Relational Operators in C programming with examples.

Relational Operators in CUsageDescriptionExample
>a > ba is greater than b7 > 3 returns true (1)
<a < ba is less than b7 < 3 returns false (0)
>=a >= ba is greater than or equal to b7 >= 3 returns true (1)
<=a <= ba is less than or equal to b7 <= 3 return false (0)
==a == ba is equal to b7 == 3 returns false (0)
!=a != ba is not equal to b7 != 3 returns true(1)

Relational Operators in C Example

This program helps you to understand the C Relational Operators practically.

For this the C Programming example, We are using two variables a and b, and their values are 9 and 4. We are going to use these two variables to perform various relational operations.

/* C Relational Operations on integers */

#include <stdio.h>

int main()
{
  int a = 9;
  int b = 4;
  
  printf(" a >  b: %d \n", a > b);
  printf("a >= b: %d \n", a >= b);
  printf("a <= b: %d \n", a <= b);
  printf("a <  b: %d \n", a < b);
  printf("a == b: %d \n", a == b);
  printf("a != b: %d \n", a != b);

}

In this relational operators example, we checked a and b values against every relational operator we have. Here one means TRUE and 0 means FALSE

Relational Operators in C example 1

Relational Operators in If Condition

This Operators example helps you to understand how relational operators in C Language used in If Condition. For this example program, We are using two variables x and y, and their values are 10 and 25. We are going to use these two variables in If Statement to check the condition using == relational operator. However, you can use any of the relational operators as the condition.

/* Using C Relational Operators in If Condition */

#include <stdio.h>

void main()
{
  int x = 10;
  int y = 25;

  if (x == y)
   {
     printf(" x is equal to y \n" );
   }

  else
   {
     printf(" x is not equal to y \n" );
   }

}
 x is not equal to y 

If x is exactly equal to y, the first printf statement will execute

 printf(" x is equal to y \n" );

When x is not equal to y, the second printf statement executed.

printf(" x is not equal to y \n" );

Here x is not equal to 25, so the Second statement is printed. Please try these relational operators in the For Loop, While loop, and do while loop as well.