# Relational Operators in C

Relational operators are used to compare values of two expressions. Relational operators are binary operators because they require two operands to operate. An expression which contains the relational operators is called relational expression. If the relation is true then the result of the relational expression is `1`, if the relation is false then the result of the relational expression is `0`.

The following table lists relational operators along with some examples:

Operator Description Example Result
`>` Greater than `1 > 2` `0`
`>=` Greater than or equal to `3 >= 2` `1`
`<` Smaller than `10 < 5` `0`
`<=` Smaller than or equal to `6 <= 7` `1`
`==` equal to `98==98` `1`
`!=` not equal to `10 != 9` `1`

In C, all non-zero values are considered as true while `0` is considered as false.

The following program demonstrates relational operators in action:

Expected Output:

## Precedence

The precedence of `<`, `<=`, `>` and `>=` operators are same and they associate from left to right. However, the precedence of `==` and `!=` is lower than other relational operators and they associate from left to right. The precedence of relational operators is lower than the arithmetic operators.

To clear things up let’s evaluate some expressions involving relational operators:

Example 1:

Step 1: Evaluate `2 * 3`.

Step 2: Evaluate `4 + 6` followed by `12 - 2`.

Step 3: `10` is not greater than `10`, so the above expression evaluates to false ( `0` ). Hence the result of the entire expression is `0`.

Result:

Example 2:

Step 1: The parentheses operator has the highest precedence and it associates from left to right. So the expression `(4 % 2 == 0)` will be evaluated first. The `%` operator has higher precedence than the equal to `==` operator. Therefore, the `%` operator will be applied first followed by the `==` operator. The expression now becomes:

Step 2: Evaluate `(8 * 2)`.

Step 3: `1` is smaller than `16`. So the above expression evaluates to true ( `1` ). Hence the result of the entire expression is true.

Result:

Don’t confuse assignment operator ( `=` ) with equal to operator ( `==` ). The first one is used to assign a value to the variable while the second one is used to test whether two values are equal or not.

To use relational operators to its full potential you must learn how to use the if-else statement. The if-else statements are discussed in detail in If… else statements in C chapter.

### 4 thoughts on “Relational Operators in C”

1. x ki value 1 aa rhi h why?

• Since you haven’t clarified, I am assuming you are having problem with the expression evaluation.

The precedence of = and == are same (see operator precedence) and they associate from right to left.So the statement x=y==z; is treated as x=(y==z);. The expression y == z evaluates to 1 and this value is then assigned to the variable x. Hence, the value of x is 1.

• bcaz aapne datatype me variable ko constant kar diya hai.

2. nice