C Programming Relational Operators

Last modified by Microchip on 2023/11/09 09:06

The relational operators are used to compare two values and tell you whether the comparison being made is true or false. For example, in the second line in the table, we are testing to see whether x is less than or equal to y. If it is true, this expression will return a non-zero value (usually 1). If the condition is false, then a value of zero will be returned. Special attention should be paid to the == (equal to) operator – note that it is not the same as the = (assignment) operator.

OperatorOperationExampleResult (FALSE=0, TRUE≠0)
<Less thanx < y1 if x less than y, else 0
<=Less than or
equal to
x <= y1 if x less than or equal to y, else 0
>Greater thanx > y1 if x greater than y, else 0
>=Greater than or
equal to
x >= y1 if x greater than or equal to y, else 0
=Equal tox == y1 if x equal to y, else 0
!=Not equal tox != y1 if x not equal to y, else 0

In conditional expressions, any non-zero value is interpreted as TRUE. A value of 0 is always FALSE.