21
NovRelational Operators in C Programming
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
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 (!=)
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 (>)
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 (<)
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(>=)
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 (<=)
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
#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;
}
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 |