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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<stdio.h> int main() { int x = 12, y = 13; printf("x = %d\n", x); printf("y = %d\n\n", y); // Is x is greater than y? printf("x > y : %d\n", x > y); // Is x is greater than or equal to y? printf("x >= y : %d\n", x >= y); // Is x is smaller than y? printf("x < y : %d\n", x < y); // Is x is smaller than or equal to y? printf("x <= y : %d\n", x <= y); // Is x is equal to y? printf("x == y : %d\n", x == y); // Is x is not equal to y? printf("x != y : %d\n", x != y); // Signal to operating system everything works fine return 0; } |

**Expected Output:**

1 2 3 4 5 6 7 8 9 |
x = 12 y = 13 x > y : 0 x >= y : 0 x < y : 1 x <= y : 1 x == y : 0 x != y : 1 |

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

1 |
4 + 2 * 3 > 12 - 2 |

Step 1: Evaluate `2 * 3`

.

1 |
4 + 6 > 12 - 2 |

Step 2: Evaluate `4 + 6`

followed by `12 - 2`

.

1 |
10 > 10 |

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:

1 |
4 + 2 * 3 > 12 - 2 => 0 |

**Example 2**:

1 |
(4 % 2 == 0) <= (8 * 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:

1 2 3 |
(4 % 2 == 0) <= (8 * 2) => (0 == 0) <= (8 * 2) => 1 <= (8 * 2) |

Step 2: Evaluate `(8 * 2)`

.

1 2 |
1 <= (8 * 2) => 1 <= 16 |

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:

1 |
(4 % 2 == 0) <= (8 * 2) => 0 |

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.

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.

nice