Relational operators < > <= >=

The relational operators compare two operands and determine the validity of a relationship. The following table describes the four relational operators:

Operator Usage
< Indicates whether the value of the left operand is less than the value of the right operand.
> Indicates whether the value of the left operand is greater than the value of the right operand.
<= Indicates whether the value of the left operand is less than or equal to the value of the right operand.
>= Indicates whether the value of the left operand is greater than or equal to the value of the right operand.

Both operands must have arithmetic or enumeration types or be pointers to the same type.

C The type of the result is int and has the values 1 if the specified relationship is true, and 0 if false. C++ The type of the result is bool and has the values true or false.

The result is not an lvalue.

If the operands have arithmetic types, the usual arithmetic conversions on the operands are performed.

When the operands are pointers, the result is determined by the locations of the objects to which the pointers refer. If the pointers do not refer to objects in the same array, the result is not defined.

A pointer can be compared to a constant expression that evaluates to 0. You can also compare a pointer to a pointer of type void*. The pointer is converted to a pointer of type void*.

If two pointers refer to the same object, they are considered equal. If two pointers refer to nonstatic members of the same object, the pointer to the object declared later is greater, provided that they are not separated by an access specifier; otherwise the comparison is undefined. If two pointers refer to data members of the same union, they have the same address value.

If two pointers refer to elements of the same array, or to the first element beyond the last element of an array, the pointer to the element with the higher subscript value is greater.

You can only compare members of the same object with relational operators.

Relational operators have left-to-right associativity. For example, the expression:

a < b <= c

is interpreted as:

(a < b) <= c

If the value of a is less than the value of b, the first relationship yields 1 in C, or true in C++. The compiler then compares the value true (or 1) with the value of c (integral promotions are carried out if needed).



[ Top of Page | Previous Page | Next Page | Contents | Index ]