Comparison operators - cppreference.com (original) (raw)

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.

Operator Operator name Example Description
== equal to a == b a is equal to b
!= not equal to a != b a is not equal to b
< less than a < b a is less than b
> greater than a > b a is greater than b
<= less than or equal to a <= b a is less than or equal to b
>= greater than or equal to a >= b a is greater than or equal to b

Contents

[edit] Relational operators

The relational operator expressions have the form

| | | | | --------------- | --- | | | lhs <** rhs | (1) | | | | | | | lhs **> rhs | (2) | | | | | | | lhs <=** rhs | (3) | | | | | | | lhs **>= rhs | (4) | | | | | |

  1. less-than expression

  2. greater-than expression

  3. less or equal expression

  4. greater or equal expression

where

lhs, rhs - expressions that both have real type or both have pointer to object type

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

If lhs and rhs are expressions of any real type, then

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

If lhs and rhs are expressions of pointer type, they must be both pointers to objects of compatible types, except that qualifications of the pointed-to objects are ignored.

#include <assert.h> int main(void) { assert(1 < 2); assert(2+2 <= 4.0); // int converts to double, two 4.0's compare equal   struct { int x,y; } s; assert(&s.x < &s.y); // struct members compare in order of declaration   double d = 0.0/0.0; // NaN assert( !(d < d) ); assert( !(d > d) ); assert( !(d <= d) ); assert( !(d >= d) ); assert( !(d == d) );   float f = 0.1; // f = 0.100000001490116119384765625 double g = 0.1; // g = 0.1000000000000000055511151231257827021181583404541015625 assert(f > g); // different values }

[edit] Equality operators

The equality operator expressions have the form

| | | | | --------------- | --- | | | lhs == rhs | (1) | | | | | | | lhs != rhs | (2) | | | | | |

  1. equal-to expression

  2. not equal to expression

where

lhs, rhs - expressions that both have any arithmetic types (including complex and imaginary) both have type nullptr_t one has type nullptr_t and the other is a null pointer constant (since C23) both are pointers to objects or functions of compatible types, ignoring qualifiers of the pointed-to types one is a pointer to object and the other is a pointer to (possibly qualified) void one is a pointer to object or function and the other is a null pointer constant such as NULL or nullptr(since C23)

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

two nullptr_t value or one nullptr_t value and a null pointer constant compare equal (since C23)

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

[edit] Notes

Objects of struct type do not compare equal automatically, and comparing them with memcmp is not reliable because the padding bytes may have any values.

Because pointer comparison works with pointers to void, the macro NULL may be defined as (void*)0 in C, although that would be invalid in C++ where void pointers do not implicitly convert to typed pointers

Care must be taken when comparing floating-point values for equality, because the results of many operations cannot be represented exactly and must be rounded. In practice, floating-point numbers are usually compared allowing for the difference of one or more units of the last place.

#include <assert.h> int main(void) { assert(2+2 == 4.0); // int converts to double, two 4.0's compare equal   int n[2][3] = {1,2,3,4,5,6}; int* p1 = &n[0][2]; // last element in the first row int* p2 = &n[1][0]; // start of second row assert(p1+1 == p2); // compare equal   double d = 0.0/0.0; // NaN assert( d != d ); // NaN does not equal itself   float f = 0.1; // f = 0.100000001490116119384765625 double g = 0.1; // g = 0.1000000000000000055511151231257827021181583404541015625 assert(f != g); // different values }

[edit] References

[edit] See also

Operator precedence

Common operators
assignment increment decrement arithmetic logical comparison member access other
a = ba += ba -= ba *= ba /= ba %= ba &= ba |= ba ^= ba <<= ba >>= b ++a --aa++a-- +a -aa + ba - ba * ba / ba % b~aa & ba | ba ^ ba << ba >> b !aa && ba | b a == ba != ba < ba > ba <= ba >= b a[b] *a &aa->ba.b