# C - Relational Operators  Written by Shuvhojit DebFebruary 7, 2022
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE Shuvhojit Deb

Full Stack Developer

## 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     You must be Logged in to comment
Code Block Shuvhojit Deb
Full Stack Developer  117 Blog Posts
Trending Technologies
15 Software40 DevOps48 Frontend Development24 Backend Development20 Server Administration17 Linux Administration26 Data Center24 Sentry24 Terraform23 Ansible83 Docker70 Penetration Testing16 Kubernetes21 NGINX20 JenkinsX17
Recommended Blogs
8        8 Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary Anik Adhikary 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.