C Relational Operators
The relational operators compare one expression to another. The answer can be only TRUE(1) or FALSE(0). The relational operators play very important role in program flow. Following is list of relational operators in C programming language.
Relational Operator | Meaning | Example | Description |
== | Equal | a == b | TRUE if a is equal to b |
!= | Not Equal | a != b | TRUE if a is not equal to b |
< | Less Than | a < b | TRUE if a is less than b |
> | Greater Than | a > b | TRUE if a is greater than b |
<= | Less Than or Equal | a <= b | TRUE if a is less than or equal to b |
>= | Greater Than or Equal | a >= b | TRUE if a is greater than or equal to b |
The relational operators are evaluated from left to right. The <, >, <=, >= has higher precedence over ==, != relational operators. In other words == and != are evaluated after the other four relational operators.
So, in a statement, 0 < 10 == 2 >= 6, first 0<10 and 2>=6 is evaluated (because of higher precedence). The result of 0 < 10 is TRUE(1) and result of 2 >= 6 is FALSE(0). Finally these results are evaluated with == operator. TRUE == FALSE, which results in FLASE(0).
0 < 10 == 2 >= 6
1 == 0
0
The arithmetic operators have higher precedence over relational operators. This means arithmetic operations are performed before evaluating the relational operators.
So, when a statement contains arithmetic and relational expressions, the c compiler evaluates precedence in the following order.
- Unary
- Multiplicative
- Additive
- <, >, <=, >=
- ==, !=
In a statement 5+5>9, the C compiler first adds 5 and 5 and then compare the result (10) with 9. In another example 9<5+5, the compiler first adds 5 and 5 (because arithmetic operators has higher precedence over relational operators) and then compare the result (10) with 9. Use parenthesis to ensure desired order of precedence.