C Programming Relational Operators

Relational operators in c programming; In this tutorial, i am going to show you relational operators in c programming and how to use relational operator in c programs.

Relational Operators In C Programming

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

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

OperatorMeaning of OperatorExample
==Equal to7 == 3 is evaluated to 0
>Greater than7 > 3 is evaluated to 1
<Less than7 < 3 is evaluated to 0
!=Not equal to7 != 3 is evaluated to 1
>=Greater than or equal to7 >= 3 is evaluated to 1
<=Less than or equal to7 <= 3 is evaluated to 0

Note that:- Relational operators are used in decision making and loops.

Example 1 – Relational Operators

// Working of relational operators
#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10;
    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);
    return 0;
}

Output

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1 

Note that:- Relational operators in C are commonly used to check the relationship between the two variables

Example 2 – C Relational Operators with If else Statement

/* 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" );
   }
}

Output

 x is not equal to y 

Be First to Comment

Leave a Reply

Your email address will not be published.