c/language/operator comparison

Comparison operators are binary operators that test a condition and return 1 if that condition is logically true and 0 if that condition is false.

Relational operators
The relational operator expressions have the form

@1@ less-than expression @2@ greater-than expression @3@ less or equal expression @4@ greater or equal expression where

The type of any relational operator expression is, and its value (which is not an lvalue) is when the specified relationship holds true and  when the specified relationship does not hold.

If and  are expressions of any, then
 * are performed
 * the values of the operands after conversion are compared in the usual mathematical sense (except that positive and negative zeroes compare equal and any comparison involving a NaN value returns zero)

Note that complex and imaginary numbers cannot be compared with these operators.

If and  are expressions of pointer type, they must be both pointers to objects of, except that qualifications of the pointed-to objects are ignored.
 * a pointer to an object that is not an element of an array is treated as if it were pointing to an element of an array with one element
 * if two pointers point to the same object, or both point one past the end of the same array, they compare equal
 * if two pointers point to different elements of the same array, the one pointing at the element with the larger index compares greater.
 * if one pointer points to the element of an array and the other pointer points one past the end of the same array, the one-past-the-end pointer compares greater
 * if the two pointers point to members of the same, the pointer to the member declared later in the struct definition compares greater than then pointer to the member declared earlier.
 * pointers to members of the same union compare equal
 * all other pointer comparisons invoke undefined behavior

Equality operators
The equality operator expressions have the form

@1@ equal-to expression @2@ not equal to expression where

The type of any equality operator expression is, and its value (which is not an lvalue) is when the specified relationship holds true and  when the specified relationship does not hold.


 * if both operands have arithmetic types, are performed and the resulting values are compared in the usual mathematical sense (except that positive and negative zeroes compare equal and any comparison involving a NaN value, including equality with itself, returns zero). In particular, values of complex type are equal if their real parts compare equal and their imaginary parts compare equal.


 * if one operand is a pointer and the other is a null pointer constant, the null pointer constant is first to the type of the pointer (which gives a null pointer value), and the two pointers are compared as described below
 * if one operand is a pointer and the other is a pointer to void, the non-void pointer is to the pointer to void and the two pointers are compared as described below
 * two pointers compare equal if any of the following is true:
 * they are both null pointer values of their type
 * they are both pointers to the same object or function
 * one pointer is to a struct/union/array object and the other is to its first member/any member/first element
 * they are both pointing one past the last element of the same array
 * one is one past the end of an array, and the other is at the start of a different array (of the same type) that follows the first in a larger array or in a struct with no padding

(as with relational operators, pointers to objects that aren't elements of any array behave as pointers to elements of arrays of size 1)