Relational Operators in C Programming

Relational Operators in C Programming

10 Jun 2024
Beginner
1.43K Views
18 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

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?

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.

Types Of Relational Operators In C

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

OperatorName
==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: 55The 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

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

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.

Relational Set Operators uses relational algebra to manipulate contents in a database.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this