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 a value 0.

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

C Relational OperatorsUsageDescriptionExample
>a > ba is greater than b7 > 3 returns true (1)
<a < ba is less than b7 < 3 returns false (0)
>=a >= ca is greater than or equal to c7 >= 3 returns true (1)
<=a <= ca is less than or equal to c7 <= 3 return false (0)
==i == ji is equal to j7 == 3 returns false (0)
!=i != ji is not equal to j7 != 3 returns true(1)

Relational Operators in C Example

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

For this 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.

#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 one of it we have. Here one means TRUE and 0 means FALSE

Relational Operators in C example 1

C Relational Operators in If Condition

This Operators example helps you to understand how relational operators in this Language are used in the 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 the If Statement to check the condition using ==. However, you can use any of the relational operators in c programming language as the 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 c 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.