# Relational Operators in C Programming

10 Jun 2024
Beginner
586 Views
Learn via Video Course & by Doing Hands-on Labs

## Relational Operators in C

Relational Operators are used to Compare two variables and understand their relationship easily. These operators are your go-to resources when determining if a score satisfies a passing grade or whether two user inputs match.

In this C Tutorial, we will talk more about relational operators in C, their types, and examples of relational operators in detail.

## What Are Relational Operators In C?

• Relational operators in C  are symbols that compare two values or expressions
• The outcome of an operation can be either true (non-zero) or False (zero).
• These operators are fundamental in controlling the flow of programs through conditions and loops.

## How do Relational Operators in C work?

The following describes how to use relational operators in C programs:

• Step 1: In this step, you input the values of the operands to be compared.
• Step 2: Next after applying the operands and relational operator you compare the values.
• Step 3: The result is a value that can be true or false depending on the comparison.
• Step 4: A programmer can then use this result to perform actions, within a statement or other control structure.
• Step 5: For example one block of code can be executed if the result is true and another block if it is false.

## Types Of Relational Operators In C

The Relational Operators are also known as Comparison Operators. They compare the values of the two operands.

There are mainly 6 relational operators in C that are as follows:

 Operator Name == Equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to != Not equal to

Let's elaborate on each relational operator in the C Compiler.

### 1. Equal to Operator (==)

The Equal to Operator verifies whether two values are identical. When the values match it outputs true; otherwise, it outputs false.

#### Syntax

`` Opr1 == Opr2      ``
Let's write a program that compares the operands' values in C using the double equal to the operator (==).

#### Example

````#include<stdio.h>    ````int main ()
{
int e = 9;
int f = 12;

// Use Equal To Operator
printf (" e == f : %d", (e == f));
if (e == f)
printf ("\n %d is equal to %d", e, f);
else
printf (" \n %d is not equal to %d", e, f);

int m = 6;
int n = 19;

// Use Equal To Operator
printf (" \n m == n : %d", (m == n));
if (e == f)
printf (" \n %d is equal to %d", m, n);
else
printf ("\n %d is not equal to %d", m, n);
return 0;
}      ``````

#### Output

`````` e == f : 0
9 is not equal to 12
m == n : 0
6 is not equal to 19  ``````

### 2. Not equal to Operator (!=)

The "Not equal, to" operator checks if two values are different returning true when they don't match and false when they do.

#### Syntax

``Opr1 != Opr2;      ``

#### Example

``````#include<stdio.h>
#include<math.h>
int main ()
{
int e = 9;
int f = 12;

// Use Not Equal To (!=) Operator
printf (" e != f : %d", (e != f));
if (e != f)
printf ("\n %d is equal to %d", e, f);
else
printf (" \n %d is not equal to %d", e, f);
int m = 15;
int n = 25;
// Use Not Equal To (!=) Operator
printf (" \n m != n : %d", (m != n));
if (e != f)
printf (" \n %d is equal to %d", m, n);
else
printf ("\n %d is not equal to %d", m, n);
return 0;
}
``````

#### Output

`````` e != f : 1
9 is equal to 12
m != n : 1
15 is equal to 25   ``````

### 3. Greater than operator (>)

Here, The Greater than operator verifies if the value on the left is greater than on the right and It returns true if this condition holds.

#### Syntax

``E > F;     ``

#### Example

``````#include<stdio.h>
int main ()
{
int var1, var2;
printf (" Enter the value of var1: ");
scanf (" %d", &var1);

printf (" \n Enter the value of var2: ");
scanf (" %d", &var2);

// use greater than operator (>)
if (var1 > var2)
{
printf (" \n The value of var1 is greater than var2.");
}
else
{
printf (" \n The value of var2 is greater than var1.");
}
return 0;
}     ``````

#### Output

``````Enter the value of var1: 11
Enter the value of var2: 34
The value of var2 is greater than var1.   ``````

### 4. Less than operator (<)

The "Less, than" operator verifies whether the value on the left is smaller than the value, on the right. It will provide a result if this comparison is accurate.

#### Syntax:

``E < F;  ``

#### Example

``````#include<stdio.h>
int main ()
{
int var1, var2;
printf (" Enter the value of var1: ");
scanf (" %d", &var1);

printf (" \n Enter the value of var2: ");
scanf (" %d", &var2);

// use less than operator (<)
if (var1 < var2)
{
printf (" \n The value of var1 is less than var2.");
}
else
{
printf (" \n The value of var2 is less than var1.");
}
return 0;
}     ``````

#### Output

``````Enter the value of var1: 45
Enter the value of var2: 55````The value of var1 is less than var2.   ````

### 5. Greater than or equal to operator(>=)

The operator greater, than, or equal to checks if the value on the side is larger than or equal to the value, on the side. It will give a result if this requirement is met.

#### Syntax

``E >= F;  ``

#### Example

`````` #include<stdio.h>
int main ()
{
int var1, var2;
printf (" Enter the value of var1: ");
scanf (" %d", &var1);

printf (" \n Enter the value of var2: ");
scanf (" %d", &var2);

// use greater than equal to operator (>=)
if (var1 > var2)
{
printf (" \n The value of var1 is greater than var2.");
}
else if (var1 >= var2 )  // greater than operator (>=)
{
printf (" \n The value of var1 is equal to var2.");
}
else
{
printf (" \n The value of var2 is greater than var1.");
}
return 0;
}      ``````

#### Output

``````Enter the value of var1: 22
Enter the value of var2: 22
The value of var1 is equal to var2.  ``````

### 6. Less than or equal to the the operator (<=)

The operator "less, than or equal to" verifies whether the value on the side is smaller than or equal to the value, on the side. It will provide a result if this requirement is met.

#### Syntax:

``E <= F;  ``

#### Example

``````#include<stdio.h>
int main ()
{
int var1, var2;
printf (" Enter the value of var1: ");
scanf (" %d", &var1);

printf (" \n Enter the value of var2: ");
scanf (" %d", &var2);

// use less than equal to operator (<=)
if (var1 < var2)
{
printf (" \n The value of var1 is less than var2.");
}
else if (var1 <= var2 )
{
printf (" \n The value of var1 is equal to var2.");
}
else
{
printf (" \n The value of var2 is less than var1.");
}
return 0;
}      ``````

#### Output

`````` Enter the value of var1: 66
Enter the value of var2: 66
The value of var1 is equal to var2.    ``````

## Example of Relational Operators in C

Here we will create a simple program in C Compiler implementing the use of relational operators:
``````#include <stdio.h>
int main() {
int a = 10, b = 20;

if (a == b) {
printf("a is equal to b\n");
} else if (a != b) {
printf("a is not equal to b\n");
}

if (a > b) {
printf("a is greater than b\n");
} else if (a < b) {
printf("a is less than b\n");
}

if (a >= b) {
printf("a is greater than or equal to b\n");
} else if (a <= b) {
printf("a is less than or equal to b\n");
}

return 0;
}
``````
This program compares the values of a and b using all six relational operators and prints the results accordingly.

#### Output

``````a is not equal to b
a is less than b
a is less than or equal to b    ``````
##### Conclusion

Understanding the syntax, types, and examples of relational operators, in C is crucial, for performing mathematical operations and manipulating data. This knowledge enables you to create optimized C programs. Also, Master your coding base in C programming to the next level by pursuing our C Programming Course.

 Similar Articles of C C Programming Assignment Operators Operators in C: Types of Operators Bitwise Operators in C: AND, OR, XOR, Shift & Complement Arithmetic Operators in C Programming
 Interview Article of C Top 50 Most Asked C Interview Questions and Answers

## FAQs

### Q1. What do you mean by relational operations?

A relational operation involves manipulating one or more tables, or relations , to result in another table.

### Q2. What are relational operators and Bitwise operators?

Relational operators: These operators, including less than, greater than, equal to, and not equal to, are utilized for comparing two operands. Bitwise operators: These operators are utilized for carrying out bitwise operations on operands like AND, OR, XOR, and NOT.

### Q3. What is a relational set operator?

Relational Set Operators uses relational algebra to manipulate contents in a database.
Share Article

### Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 Azure Developer Certification Training Sep 14 SAT, SUN Filling Fast10:00AM to 12:00PM (IST) ASP.NET Core Certification Training Sep 15 SAT, SUN Filling Fast09:30AM to 11:30AM (IST) Advanced Full-Stack .NET Developer Certification Training Sep 15 SAT, SUN Filling Fast09:30AM to 11:30AM (IST) .NET Solution Architect Certification Training Sep 22 SAT, SUN Filling Fast07:00AM to 09:00AM (IST) Software Architecture and Design Training Sep 22 SAT, SUN Filling Fast07:00AM to 09:00AM (IST) Advanced Full-Stack .NET Developer Certification Training Sep 29 SAT, SUN Filling Fast08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Sep 29 SAT, SUN Filling Fast08:30PM to 10:30PM (IST)

Can't find convenient schedule? Let us know