In C language Operator Groups are present. There are seven types of operators present:
- Unary
- Arithmetic
- Bitwise
- Relational
- Logical
- Conditional
- Assignment
In C, a preceding rule that exists in operator Groups. If in a problem there are multiple operators present, then this type of problem is solved according to this order of operator groups.
Relational operator is the member of this operator groups. There are many types of relational operators present in C language. They are Lesser than ( < ), Greater than ( > ), Lesser equal ( <= ), Greater equal ( >= ), equals ( == ), not equal ( != ).
<, >, <=, >= are first priority.
==, != are second priority.
This is associatively rule from Left to Right.
Remember:
Relational operator always yields result either 0 or 1. Every nonzero value is True or False. True is 1 and false is 0.
Programming Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
As, priority of the Relational Operator ( > ) is greater than in the priority of assignment operator ( = ). So, here first execute ( 3 > 4 ). It is false statement. So, 0 is assigned in x. So, output of the result = 0.
Programming Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
Explanation:
The above programming example is also another example of relational operator. Here, we solve another expression:
1 | x = 3 <= 4 ; |
Here, two operators are used. One is assignment operator ( = ), another is relational operator lesser equal ( <= ). As Relational operator is higher priority than assignment operator, relational operator executes firstly.
Is 3 < or = of 4? Yes 3 < 4. So, it is true. 1 is assigned in x. So, output of the result=1.
Programming Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
Explanation:
The above programming example is also another example of relational operator. Here, we solve another expression. The expression is:
1 | x= 4!=3 ; |
Here, two operators are used. One is assignment operator ( = ), another is relational operator not equal ( != ). As Relational operator is higher priority than assignment operator, relational operator executes first.
Is 4!= 3? Yes, 4 is not equal to 3. So, it is true. 1 is assigned in x. So, output of the result = 1.
Programming Example 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
Explanation:
The above programming example is also another example of relational operator. Here we solve another expression. The expression is:
1 | x= 5 >4 >3 ; |
Here, three operators are used. One is assignment operator ( = ), rest of the two are relational operator greater than( > ). As Relational operator is higher priority than assignment operator, relational operator executes first. But question is that here two same relational operators greater than ( >) are used twice, so which greater than ( > ) executes first? The rule is, any operators which is used multiple times or same priority operators are used multiple times. In any expression, associvity rules from left to right is applied here.
So, 5 > 4 executes first. Is 5 > 4? Yes, 5 is greater than 4. So, the result is 1 (true). Now 1 > 3 executes. Is 1 > 3? 1 is not greater than 3. So it is false. So, 0 is assigned in x, means assignment operator executes here. So, output of the result = 0.
Programming Example 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Output:
Explanation:
Here, we show another example of relational operator. The given expression uses in the programming example is:
1 | x = 12 < 4 ; |
Here, two operators are used. One is assignment operator ( = ), another is relational operator not equal ( != ). As Relational operator is higher priority than assignment operator, relational operator executes first.
Is twelve is lesser than four? No! Twelve is not lesser than four. So, the result is 0. Now, 0 is assigned to variable x with the help of assignment operator ( = ). So, the output will be zero.
Programming Example 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
Explanation:
Here, we give another example of relational operator. The given expression is:
1 | x = 6 >= 4; |
Here, two operators are used. One is assignment operator ( = ), another is relational operator greater equal ( >= ). As Relational operator is higher priority than assignment operator, relational operator executes first.
Is six is greater equal four? Yes! Six is greater than four but not equal to four. Between two conditions, one is true. As one condition is true, it is overall true. So, the result is 1. Now, 1 is assigned to variable x with the help of assignment operator ( = ). So, the output will be one.
Programming Example 7:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Output:
Explanation:
The above programming example is also another example of relational operator. Here, we solve another expression. The expression is:
[cc lang=”c” width=”100%” height=”100%” escaped=”true” theme=”blackboard” nowrap=”0″ line_numbers=”on”]x= 9 ==8 ;
Here, two operators are used. One is assignment operator ( = ), another is relational operator equal ( == ). As Relational operator is higher priority than assignment operator, relational operator executes firstly
Is 9 == 8? 9 is not equal to 8. So, it is false. So, 0 is assigned in x. So, the result = 0.
Conclusion:
This is a discussion on different types of programming example of relational operator. It is on how relational operator works or what will be its output. Basically relational operators gives us an output either true ( 1 ) or false ( 0 ).