C - Relational Operators

C - Relational Operators
Techiio-author
Written by Shuvhojit DebFebruary 7, 2022
21 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this tutorial, you'll learn everything about relational operators.

Relational Operators in C

Relational operators are those which are part of Binary operators. These operators are generally used in the comparison between two values or conditions. This comparison condition drives us to the Boolean expression values by which the code written is accordingly executed.

Types of Relational Operators

1. Greater than (>)

The operator checks the value of the left operand is greater than the right operand, and if the statement is true, the operator is said to be the Greater Than Operator.

Syntax:

A > B;  

Here, operand A is greater than operand B.

Let's create a program to use the greater than operator (>) to compare the operand value in C.

Example:

#include <stdio.h>  
  
int main ()  
{  
    int num1, num2;  
    printf (" Enter the value of num1: ");  
    scanf (" %d", &num1);  
      
    printf (" \n Enter the value of num2: ");  
    scanf (" %d", &num2);  
      
    // use greater than operator (>)   
    if (num1 > num2)  
    {  
        printf (" \n The value of num1 is greater than num2.");  
    }  
    else  
    {  
        printf (" \n The value of num2 is greater than num1.");  
    }  
    return 0;  
}  

Output:

Enter the value of num1: 20
Enter the value of num2: 30
The value of num2 is greater than num1.

2. Less than (<)

It is used to check whether the value of the left operand is less than the right operand, and if the statement is true, the operator is known as the Less than Operator.

Syntax:

A < B;  

Here operand A is less than operand B.

Let's create a program to use the less-than operator (<) to compare the operand value in C.

Example:

#include <stdio.h>  
  
int main ()  
{  
    int num1, num2;  
    printf (" Enter the value of num1: ");  
    scanf (" %d", &num1);  
      
    printf (" \n Enter the value of num2: ");  
    scanf (" %d", &num2);  
      
    // use less than operator (<)   
    if (num1 < num2)  
    {  
        printf (" \n The value of num1 is less than num2.");  
    }  
    else  
    {  
        printf (" \n The value of num2 is less than num1.");  
    }  
    return 0;  
}  

Output:

Enter the value of num1: 45
Enter the value of num2: 89
The value of num1 is less than num2.

3. Greater than and equal to (>=)

The operator checks whether the left operand's value is greater than or equal to the right operand. If the statement is true, the operator is said to be the Greater than Equal to Operator.

Syntax:

A >= B;  

Here, operand A is greater than equal to the right operand B. So, it is the Greater than Equal To operator.

Let's create a program to use the Greater than Equal To operator to compare the operand value in C.

Example:

#include <stdio.h>  
  
int main ()  
{  
    int num1, num2;  
    printf (" Enter the value of num1: ");  
    scanf (" %d", &num1);  
      
    printf (" \n Enter the value of num2: ");  
    scanf (" %d", &num2);  
      
    // use greater than equal to operator (>=)   
    if (num1 > num2)  
    {  
        printf (" \n The value of num1 is greater than num2.");  
    }  
    else if (num1 >= num2 )  // greater than operator (>=)  
    {  
        printf (" \n The value of num1 is equal to num2.");  
    }  
    else   
    {  
        printf (" \n The value of num2 is greater than num1.");  
    }  
    return 0;  
}  

Output:

Enter the value of num1: 28
Enter the value of num2: 79
The value of num2 is greater than num1.

4. Less than and equal to (<)

The operator checks whether the value of the left operand is less than or equal to the right operand, and if the statement is true, the operator is said to be the Less than Equal To Operator.

Syntax:

A <= B;  

Here, operand A is less than or equal to operand B. So, it is a Less than Equal to Operator.

Let's create a program to use the Less than Equal to an operator (<=) to compare the operand value in C.

Example:

#include <stdio.h>  
  
int main ()  
{  
    int num1, num2;  
    printf (" Enter the value of num1: ");  
    scanf (" %d", &num1);  
      
    printf (" \n Enter the value of num2: ");  
    scanf (" %d", &num2);  
      
    // use less than equal to operator (<=)   
    if (num1 < num2)  
    {  
        printf (" \n The value of num1 is less than num2.");  
    }  
    else if (num1 <= num2 )  
    {  
        printf (" \n The value of num1 is equal to num2.");  
    }  
    else   
    {  
        printf (" \n The value of num2 is less than num1.");  
    }  
    return 0;  
}  

Output:

Enter the value of num1: 45
Enter the value of num2: 45
The value of num1 is equal to num2.

5. Double equal to (==)

It is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal.

Syntax:

Opr1 == Opr2  

Let's create a program to use the double equal to the operator (==) for comparing the value of the operands in C.

Example:

#include <stdio.h>  
#include <math.h>  
int main ()  
{  
    int a = 5;  
    int b = 10;  
      
    // Use Equal To Operator  
    printf (" a == b : %d", (a == b));  
    if (a == b)  
        printf ("\n %d is equal to %d", a, b);  
    else  
        printf (" \n %d is not equal to %d", a, b);  
  
      
    int x = 5;  
    int y = 5;  
      
    // Use Equal To Operator  
    printf (" \n x == y : %d", (x == y));  
    if (a == b)  
        printf (" \n %d is equal to %d", x, y);  
    else  
        printf ("\n %d is not equal to %d", x, y);  
    return 0;     
}     

Output:

a == b : 0
5 is not equal to 10
x == y : 1
5 is not equal to 5

6. Not Equal to (!=)

The Not Equal To Operator is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.

Syntax:

Opr1 != Opr2;  

Let's create a simple program to use the Not equal to (!=) operator for comparing the values of the variable in C.

Example:

#include <stdio.h>  
#include <math.h>  
int main ()  
{  
    int a = 5;  
    int b = 10;  
      
    // Use Not Equal To (!=) Operator  
    printf (" a != b : %d", (a != b));  
    if (a != b)  
        printf ("\n %d is equal to %d", a, b);  
    else  
        printf (" \n %d is not equal to %d", a, b);  
  
      
    int x = 5;  
    int y = 5;  
      
    // Use Not Equal To (!=) Operator  
    printf (" \n x != y : %d", (x != y));  
    if (a != b)  
        printf (" \n %d is equal to %d", x, y);  
    else  
        printf ("\n %d is not equal to %d", x, y);  
    return 0;     
}  

Output:

a != b : 1
5 is equal to 10
x != y : 0
5 is equal to 5

Conclusion

So, this is how we can use different relational operators. These operations play a very important role in any programming language. And here we had done the same in the C programming language.

C Language
Software
Relational Operator
Program
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance