C Relational Operators Basic C Programming

C Relational Operators

In this tutorial, we will be focusing on understanding the Relational Operators. These operators are used to show the order relation between two expressions. In fact, they are mainly used for the comparison between two expressions. the entire expression which involves two expressions along with the relational operator is called the relational expression. On the basis of that comparison, they produce two kinds of results either true or false. If the condition is true then the value of the relational expression will be true (1). If the condition is false then the value of the relational expression will be false (0). A list of Relational operators is given below.

S. No.Relational OperatorsMeaning
1.= =Equal to
2.! =Not Equal to
3.Greater Than
4.Less Than
5.>=Greater Than OR Equal to
6.<=Less Than OR Equal to

Let ‘x’ and ‘y’ are two variables such that x = 2 and y = 5, then the values of relational expression are: 

S. No.Relational ExpressionResultValue
1.x = = yFalse0
2.x! = yTrue1
3.x > yFalse0
4.x < yTrue1
5.x >= yFalse0
6.x <= yTrue1

C Relational Operators Working

The C relational operators mainly used with selection keywords like if and else. Since we have not yet discussed anything about these selection keywords. But here I would like to give you a basic working of this selection keywords because we will discuss the manipulation of the relational operators with an example. Now because the relational operators are used with if and else. To better understand the example on relational operators we will take an overview of if and else. In the ” C Control Statements” tutorial we will discuss if and else in greater detail. The basic syntax of if and else:

if(Relational Expression)
{
Program Statement 1;
}
else
{
Program Statement 2;
}

Explanation: If the value of the relational expression is true, then the control will be passed to the ‘if’ block, and the program statement 1 will be executed. If the value of the relational expression is false then the if block will be skipped and the control will be passed to the else block and the program statement 2 will be executed.

Example:
#include<stdio.h>
main()
{
int x, y;
printf("x and y are: ");
scanf("%d%d", &x,&y);
if(x==y)
printf("Marvel\n");
if(x!=y)
printf("Avengers\n");
if(x>y)
printf("Tony Stark\n");
if(x<y)
printf("Captain Rogers\n");
}
Output:
x and y are: 2 5
Avengers
Captain Rogers

Difference between Assignment (=) and Equality (==) Operators

In case by mistake, we write in the above program (=) instead of (==) then the value of y will be assigned to the variable ‘x’. Now the condition in this control statement will be true since ‘x’ has a non zero value (5). So in this case that if the block will be executed and we will get different and incorrect outputs. Here in this case the output will be

Output:
x and y are: 2 5
Marvel
Avengers
Captain Rogers

I this tutorial we have learned and understood the working of C Relational Operators. In the next tutorial, we will be discussing “Logical Operators in C”.

Leave a Comment

Your email address will not be published. Required fields are marked *