Relational Operators in C with Examples with explanation. Relational operators(<,<=,>,>=,==,=!) Tutorials, Example programs
Relational Operators :
 Relational operators used to compare values of two Expressions depending upon their Relation.
 If the Relation is True, relation operator give result as true i.e 1.
 If the Relation is False, then Relational operator give result as False i.e 0.
 Relational Operators output is always 0 (False) or 1(True) only.
C Language have six relational operators those are..
Operator

Meaning

<

Less than

<=

Less than or Equal to

==

Equal to

!=

Not Equal to

>

Greater than

>=

Greater than or Equal to

Example to Understand Relational Operators in C :
Now we will take one Example to understand more about Relational Operators. take two variables let me name it as A and B. assign some Random values A = 10 and B = 20. Now Compare the A and B by using Relational operators.
Expression

Relation

Output (or)Result

A < B

True

1

A <= B

True

1

A == B

False

0

A != B

True

1

A > B

False

0

A >= B

False

0

Program to Understand Relational operators :

#include <stdio.h>

int main(void)

{

int a,b;

a=10; b=20;

printf(“Relational Operators output n“);

printf(“a < b is : %d n“,a<b);

printf(“a <= b is : %d n“,a<=b);

printf(“a == b is : %d n“,a==b);

printf(“a != b is : %d n“,a!=b);

printf(“a > b is : %d n“,a>b);

printf(“a >= b is : %d n“,a>=b);

return 0;

}
Output :
Relational Operators output
a < b is : 1
a <= b is : 1
a == b is : 0
a != b is : 1
a > b is : 0
a >= b is : 0
Program to Understand How Relational operators Behavior :

#include <stdio.h>

int main(void) {

int a = 1,b = 2,c = 3;

int d;

d = a > b < c;

printf(“The Value of d is : %d n“,d);

return 0;

}
Output :
The Value of d is : 1
Explanation :
We already know that relational operators result is true or false only. so coming to our Example
d value is equal to a > b < c.
i.e d = 1 > 2 < 3;
step 1:
If we have more than one operator in expression is calculated based on priority of operators.here two operators have same priority so calculation starts from left to right because all binary operators associtivity is Left to right. for entire priority table you can refer Precedence and Associativity of Operators.
d = 1 > 2 is false so result is 0.
step 2 :
d = 0 < 3 is true so result is 1.and overall result also one.
Note : Assignment operator and Equality operator is entirely Different. Assignment operator(=) is used to Assign Values while Equality(==) operator is used to compare the Two Expressions.
Example to understand Difference between Equality operator(==) and assignment operator(=) :

#include <stdio.h>

int main(void)

{

int a = 100, b = 200;

printf(“a==b is : %d n“,a==b);

printf(“a=b is : %d n“,a=b);

return 0;

}
Output :
a==b is : 0
a=b is : 200
Also See :
 Arithmetic Operators with Examples.
 Arithmetic operators priority and it’s Associativity.
 Modulus Operator and Hidden Concepts of Modulus Operator.
 Precedence Table or Operators Priority Table.
 Assignment Operator, Usage, Examples
 Increment Operator and Different types of Increment operators Usage with Examples.
 Decrement Operator and Different types of Decrement operators with Examples.
2 Responses
[…] Relational Operators. […]
[…] Relational Operators. […]