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 |
The relational operator expressions have the form.
lhs < rhs | (1) | |
lhs > rhs | (2) | |
lhs <= rhs | (3) | |
lhs >= rhs | (4) |
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) ); float f = 0.1; // f = 0.100000001490116119384765625 double g = 0.1; // g = 0.1000000000000000055511151231257827021181583404541015625 assert(f > g); // different values }
The equality operator expressions have the form.
lhs == rhs | (1) | |
lhs != rhs | (2) |
where.
lhs, rhs | - | expressions that
|
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.
(as with relational operators, pointers to objects that aren't elements of any array behave as pointers to elements of arrays of size 1).
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 }
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement | arithmetic | logical | comparison | member access | other |
|
|
|
|
|
|
|
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/language/operator_comparison