Relational Operators in C with Examples with explanation.| Relational operators(<,<=,>,>=,==,=!) Tutorials, Example programs

Relational Operators in C Language :

  • 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 :

  1. #include <stdio.h>
  2. int main(void) 
  3. {
  4.         int a,b;
  5.         a=10; b=20;
  6.         printf(“Relational Operators output n);
  7.         printf(“a < b is : %d \n,a<b);
  8.         printf(“a <= b is : %d \n,a<=b);
  9.         printf(“a == b is : %d \n,a==b);
  10.         printf(“a != b is : %d \n,a!=b);
  11.         printf(“a > b is : %d \n,a>b);
  12.         printf(“a >= b is : %d \n,a>=b);
  13.         return 0;
  14. }

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

Note : It is Important to understand Normal Mathematical Relations and C programming Relational operators relation is Different. If we have more than one relational operator in Expression it will calculate according to Precedence and Here true is 1 and False is 0. Lets observe this example.

Program to Understand How Relational operators Behavior  :

  1. #include <stdio.h>
  2. int main(void) {
  3.         int a = 1,= 2,= 3;
  4.         int d;
  5.         d = a > b < c;
  6.         printf(“The Value of d is : %d \n,d);
  7.         return 0;
  8. }

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(=) :

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.         int a = 100, b = 200;
  5.         printf(“a==b is : %d \n,a==b);
  6.         printf(“a=b is : %d \n,a=b);
  7.         return 0;
  8. }

Output :

a==b is : 0
a=b is : 200

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. Brindha says:

    What is difference between ” % 5.4d” and “%-5d ” in relational operators

Leave a Reply