C Programming

# Relational Operators in C

In C language Operator Groups are present. There are seven types of operators present:

1. Unary
2. Arithmetic
3. Bitwise
4. Relational
5. Logical
6. Conditional
7. 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:

 12345678910111213 #include int main () { int x ; x = 3 > 4 ; printf ( " %dÂ  ", x ) ; }

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

 12345678910111213 #include int main () { intÂ  x ; x = 3 <= 4 ; printf ( " %d ",Â  x ); }

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

 12345678910111213 #include int main () { int x; x= 4!=3; printf (" %d ", x); }

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

 12345678910111213 #include int main () { int x; x= 5>4>3; printf (" %d ", x); }

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

 123456789101112131415 #include int main () { int x; x = 12 < 4; if (x==0) printf (" %d ", x); }

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

 12345678910111213 #include int main () { int x; x = 6 >= 4; printf (" %d ", x); }

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

 12345678910111213 #include int main () { int x; x = 9 == 8; printf (" %d ", x); }

## 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 ).

### About the author

#### Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com