Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Relational and equality operators in C

Reference for C's relational (<, >, <=, >=) and equality (==, !=) operators: the int result, signed versus unsigned traps, floats, pointers and strings.

C has four relational operators, <, >, <= and >=, and two equality operators, == and !=. Each compares two operands and produces an int with the value 1 (true) or 0 (false). Since C23 the result is still int, even though bool, true and false are now keywords.

Comparison looks trivial, but the usual arithmetic conversions apply to the operands first, and that causes the best-known C surprises: comparing a negative int with an unsigned value, comparing floating-point results for equality, and trying to compare strings with ==.

C
if (age >= 18 && age != 21) { /* ... */ }

Try it

C Results are 0 or 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Pitfalls: signed/unsigned, floats and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
a <  b     a >  b     a <= b     a >= b      /* relational */
a == b     a != b                              /* equality   */
Operator Meaning Allowed operands
<, >, <=, >= ordering Arithmetic types, or two pointers to the same array object (or one past its end).
==, != equality Arithmetic types, pointers to compatible types, a pointer and a null pointer constant, or a pointer and void *.

Result: int 1 or 0. Evaluation: operands are converted first (usual arithmetic conversions). The equality operators have lower precedence than the relational ones, and both group left to right.

Errors / undefined behavior: relational comparison of pointers to different objects is undefined. Comparing a pointer that was passed to free is also best avoided.

Usage notes

  • a < b < c is not a range check. It parses as (a < b) < c. Write a < b && b < c.
  • Signed and unsigned operands: the signed value converts to unsigned, so -1 < 1u is false. Enable -Wsign-compare, or convert both sides to a common signed type after range-checking.
  • Floating-point equality is fragile. Results of arithmetic rarely compare exactly equal. Compare with a tolerance, see comparing floating-point numbers.
  • NaN compares unequal to everything, including itself; x != x is a (non-obvious) NaN test, and isnan(x) is clearer.
  • Strings: == compares pointers. Use strcmp, strncmp or memcmp (see strcmp).
  • Struct comparison does not exist. p1 == p2 on structs is a compile error. Compare member by member; memcmp is unreliable because of padding.
  • Do not compare against true: a true value from other functions may be any non-zero number. Write if (flag), not if (flag == true).
  • Yoda conditions (5 == x) turn a mistaken = into a compile error, though modern compilers' warnings make them unnecessary.
  • Null checks: p == NULL and !p are equivalent for pointers.