From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Foperator_comparison.html | 149 ++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 devdocs/c/language%2Foperator_comparison.html (limited to 'devdocs/c/language%2Foperator_comparison.html') diff --git a/devdocs/c/language%2Foperator_comparison.html b/devdocs/c/language%2Foperator_comparison.html new file mode 100644 index 00000000..cd1e66ad --- /dev/null +++ b/devdocs/c/language%2Foperator_comparison.html @@ -0,0 +1,149 @@ +

Comparison operators

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

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
+}

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 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.

+ + +
(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)

+

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
+}

References

See also

Operator precedence

+ + + +
Common operators
assignment increment
decrement
arithmetic logical comparison member
access
other

a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b

+

++a --a a++ a--

+

+a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b

+

!a a && b a || b

+

a == b a != b a < b a > b a <= b a >= b

+

a[b] *a &a a->b a.b

+

a(...) a, b (type) a a ? b : c sizeof

_Alignof
(since C11)

+
+
C++ documentation for Comparison operators
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/operator_comparison +

+
-- cgit v1.2.3