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 OperatorMeaningExampleDescription
==Equala == bTRUE if a is equal to b
!=Not Equala != bTRUE if a is not equal to b
<Less Thana < bTRUE if a is less than b
>Greater Thana > bTRUE if a is greater than b
<=Less Than or Equala <= bTRUE if a is less than or equal to b
>=Greater Than or Equala >= bTRUE 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.

  1. Unary
  2. Multiplicative
  3. Additive
  4. <, >, <=, >=
  5. ==, !=

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.