Relational Operators in C

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

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

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 Relational Operators practically. For this C Programming tutorial 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 Example

C Relational Operators in If Condition

This Operators example helps you understand how relational operators in this Language are used in the if-else 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 this 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" );
   }

}
Relational Operators inside IF Else Statement Example

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 is 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, do while loop, and else if as well.