Relational Operators in C Programming In the previous series of
C tutorials we have learn About Logical operator
Had to go about and before Known about the operators. In which I told that posts
will be written in different details about all the operators one by one, which
will be with the programming. So in today's tutorial, I will tell you what is Relational
Operators in C Programming.
//check condition x is equal to Y
if(x == y)
if(x != y)
Relational Operators in C Programming
Relational
Operators are
used to relate two variables or two conditions with each other, used to compare
mean, hence it is also called comparison operator. This operator is used to
check the relationship between two variables, constants or operands.
If we take an example of real life and explain to you
about the relational operator, then you will be able to understand very easily.
For example, if a company has vacancy for a post in any company and requires minimum
60% marks in Graduation for that, then here you will first
compare 60% with your marks, which will be compared only
by the relational operators. Like 78%> = 60%.
In
C Programming, the relational operator provides only two values. 0 and
1. 0 means False means that the condition is false and 1 means True.
In
the table below, all the Relational Operators are shown, which can be
used in C Programming.
Operator
|
Description
|
==
|
This operator is called equal to operator. This
operator is used to equal check two values. If both values are equal then
it returns true.
|
!=
|
This operator is called Not equal to operator. It is
used to check that two operands are not equal. Meaning this operator is used
to check the value of two operands, if both operands do not have equal value
then it returns true.
|
>
|
This operator is called Greater than operator. It is
used to check the value of the first operand more than the value of the
second operand. If the value of the first operand is greater than the value
of the second operand then it returns true like (5> 2) return true
|
<
|
This operator is called Less than operator. It is used
to check the value of the first operand less than the value of the second
operand. If the value of the first operand is smaller than the value of the
second operand then it returns true such as (3 <4) return true
|
>=
|
This operator is called Greater than equal to operator.
It is used to check the value of the first operand greater than and equal to
the value of the second operand. If the value of the first operand is greater
than or equal to the value of the second operand, it returns true such that (5> = 5) return true
|
<=
|
This operator is called Less than equal to operator. It
is used to check the value of the first operand less than the value of the
second operand. If the value of the first operand is smaller than or equal to
the value of the second operand, it returns true such that (5 <=
5) return true
|
Example of Relational Operators in C
with Program
In the given program, we have used two integer type variables x and y,
where the value of x is 25 and the value of y is 6. Using these two variables, we have made a calculation show of
different Relational Operators here, whose output you can see below the
program.
/** Relational
operator example in C **/
#include
<stdio.h>
int main()
{
int x = 25;
int y = 6;
printf("x < y: %d \n", x < y);
printf("x <= y: %d \n", x <=
y);
printf("x >= y: %d \n", x >=
y);
printf("x > y: %d \n", x > y);
printf("x == y: %d \n", x == y);
printf("x != y: %d \n", x != y);
}
Output
x < y: 0
x <= y: 0
x >= y: 1
x > y: 1
x == y: 0
x != y: 1
Explanation of the above program
I have checked all the conditions in the same line in
the next line, if according to the Relational Operators the condition is
correct then 1 will be print otherwise 0
will be print.
Example of relational operator by If condition
Now we will tell you the example of Relational
Operators using if condition. We will read about the If condition in the
subsequent tutorial.
In the given program, I am declare and initialize two
variables x and y. By which I will check the condition and print different
output accordingly.
The same question continues in the minds of many new programmers that after
all where all these will be used.
Hello readers, let me tell you that Relational
Operators and If condition are used when you have to check a condition that
gives different types of output like if you want to check whether Ram is worth
voting for.
So you will check the condition whether Ram's age is equal to 18 or bigger. If it is, then it is able to vote otherwise it
cannot vote.
/** Relational
Operator Example With If Conditon In C Programming **/
#include
int main()
{
int x = 25;
int y = 6;
//check condition x is greater than Y
if(x > y)
{
//true section
printf("X is greater than
Y\n");
}
else
{
//false section
printf("X is not greater than
Y\n");
}
if(x >= y)
{
printf("X is greater than or equal
to Y\n");
}
else
{
printf("");
}
}
//check condition x is less than Y
if(x < y)
{
printf("X is less than Y\n");
}
else
{
printf("X is not less than
Y\n");
}
//check condition x is equal to Y
if(x == y)
{
printf("X is equal to Y\n");
}
else
{
printf("X is not equal than
Y\n");
}
//check condition x is not equal to Y
if(x != y)
{
printf("X is not equal to
Y\n");
}
else
{
printf("X is equal than Y\n");
}
}
Output
X is greater than
Y
X is greater than or
equal to Y
X is not less than
Y
X is not equal than
Y
X is not equal to
Y
Explanation of the above program
In the above program, I have given an
example of Relational Operators by If condition. In which two variables
x and y are declare and initialize. Whose values are 25
and 6 respectively.
• I have checked a condition in every
if condition, if that condition is true then true section will print otherwise
false section will print.
Post a Comment
Please do not any spam link in the comment