diff options
Diffstat (limited to 'devdocs/c/language%2Foperator_comparison.html')
| -rw-r--r-- | devdocs/c/language%2Foperator_comparison.html | 149 |
1 files changed, 0 insertions, 149 deletions
diff --git a/devdocs/c/language%2Foperator_comparison.html b/devdocs/c/language%2Foperator_comparison.html deleted file mode 100644 index cd1e66ad..00000000 --- a/devdocs/c/language%2Foperator_comparison.html +++ /dev/null @@ -1,149 +0,0 @@ - <h1 id="firstHeading" class="firstHeading">Comparison operators</h1> <p>Comparison operators are binary operators that test a condition and return <b>1</b> if that condition is logically <b>true</b> and <b>0</b> if that condition is <b>false</b>.</p> -<table class="wikitable"> <tr style="text-align:center"> <th> Operator </th> <th> Operator name </th> <th> Example </th> <th> Description </th> -</tr> <tr style="text-align:center"> <td> <code>==</code> </td> <td> equal to </td> <td> <code>a == b</code> </td> <td> <b>a</b> is equal to <b>b</b> </td> -</tr> <tr style="text-align:center"> <td> <code>!=</code> </td> <td> not equal to </td> <td> <code>a != b</code> </td> <td> <b>a</b> is not equal to <b>b</b> </td> -</tr> <tr style="text-align:center"> <td> <code><</code> </td> <td> less than </td> <td> <code>a < b</code> </td> <td> <b>a</b> is less than <b>b</b> </td> -</tr> <tr style="text-align:center"> <td> <code>></code> </td> <td> greater than </td> <td> <code>a > b</code> </td> <td> <b>a</b> is greater than <b>b</b> </td> -</tr> <tr style="text-align:center"> <td> <code><=</code> </td> <td> less than or equal to </td> <td> <code>a <= b</code> </td> <td> <b>a</b> is less than or equal to <b>b</b> </td> -</tr> <tr style="text-align:center"> <td> <code>>=</code> </td> <td> greater than or equal to </td> <td> <code>a >= b</code> </td> <td> <b>a</b> is greater than or equal to <b>b</b> </td> -</tr> -</table> <h3 id="Relational_operators"> Relational operators</h3> <p>The relational operator expressions have the form</p> -<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code><</code> <span class="t-spar">rhs</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td> -</tr> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code>></code> <span class="t-spar">rhs</span> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td> -</tr> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code><=</code> <span class="t-spar">rhs</span> </td> <td> (3) </td> <td class="t-sdsc-nopad"> </td> -</tr> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code>>=</code> <span class="t-spar">rhs</span> </td> <td> (4) </td> <td class="t-sdsc-nopad"> </td> -</tr> -</table> <div class="t-li1"> -<span class="t-li">1)</span> less-than expression</div> <div class="t-li1"> -<span class="t-li">2)</span> greater-than expression</div> <div class="t-li1"> -<span class="t-li">3)</span> less or equal expression</div> <div class="t-li1"> -<span class="t-li">4)</span> greater or equal expression <p>where</p> -<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">lhs</span>, <span class="t-spar">rhs</span> </td> <td> - </td> <td> expressions that both have real type or both have pointer to object type </td> -</tr> -</table> -</div> <p>The type of any relational operator expression is <code>int</code>, and its value (which is not an lvalue) is <code>1</code> when the specified relationship holds true and <code>0</code> when the specified relationship does not hold.</p> -<p>If <span class="t-spar">lhs</span> and <span class="t-spar">rhs</span> are expressions of any <a href="types" title="c/language/types" class="mw-redirect">real type</a>, then</p> -<ul> -<li> <a href="conversion#Usual_arithmetic_conversions" title="c/language/conversion">usual arithmetic conversions</a> are performed </li> -<li> 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) </li> -</ul> <p>Note that complex and imaginary numbers cannot be compared with these operators.</p> -<p>If <span class="t-spar">lhs</span> and <span class="t-spar">rhs</span> are expressions of pointer type, they must be both pointers to objects of <a href="types#Compatible_types" title="c/language/types" class="mw-redirect">compatible types</a>, except that qualifications of the pointed-to objects are ignored.</p> -<ul> -<li> 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 </li> -<li> if two pointers point to the same object, or both point one past the end of the same array, they compare equal </li> -<li> if two pointers point to different elements of the same array, the one pointing at the element with the larger index compares greater. </li> -<li> 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 </li> -<li> if the two pointers point to members of the same <a href="struct" title="c/language/struct">struct</a>, the pointer to the member declared later in the struct definition compares greater than then pointer to the member declared earlier. </li> -<li> pointers to members of the same union compare equal </li> -<li> all other pointer comparisons invoke undefined behavior </li> -</ul> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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 -}</pre></div> </div> <h3 id="Equality_operators"> Equality operators</h3> <p>The equality operator expressions have the form</p> -<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code>==</code> <span class="t-spar">rhs</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td> -</tr> <tr class="t-sdsc"> <td> <span class="t-spar">lhs</span> <code>!=</code> <span class="t-spar">rhs</span> </td> <td> (2) </td> <td class="t-sdsc-nopad"> </td> -</tr> -</table> <div class="t-li1"> -<span class="t-li">1)</span> equal-to expression</div> <div class="t-li1"> -<span class="t-li">2)</span> not equal to expression <p>where</p> -<table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">lhs</span>, <span class="t-spar">rhs</span> </td> <td> - </td> <td> expressions that <ul><li> both have any <a href="arithmetic_types" title="c/language/arithmetic types">arithmetic types</a> (including complex and imaginary) </li></ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c23"> -<td> <ul> -<li> both have type <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code> </li> -<li> one has type <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code> and the other is a null pointer constant </li> -</ul> </td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> -</tr> </table> <ul> -<li> both are pointers to objects or functions of <a href="types#Compatible_types" title="c/language/types" class="mw-redirect">compatible</a> types, ignoring qualifiers of the pointed-to types </li> -<li> one is a pointer to object and the other is a pointer to (possibly qualified) <code>void</code> </li> -<li> one is a pointer to object or function and the other is a null pointer constant such as <code><a href="../types/null" title="c/types/NULL">NULL</a></code> <span class="t-rev-inl t-since-c23"><span>or <code>nullptr</code></span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> </li> -</ul> </td> -</tr> -</table> -</div> <p>The type of any equality operator expression is <code>int</code>, and its value (which is not an lvalue) is <code>1</code> when the specified relationship holds true and <code>0</code> when the specified relationship does not hold.</p> -<ul><li> if both operands have arithmetic types, <a href="conversion#Usual_arithmetic_conversions" title="c/language/conversion">usual arithmetic conversions</a> 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. </li></ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c23"> -<td> <ul><li> two <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code> value or one <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code> value and a null pointer constant compare equal </li></ul> </td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> -</tr> </table> <ul> -<li> if one operand is a pointer and the other is a null pointer constant, the null pointer constant is first <a href="conversion" title="c/language/conversion">converted</a> to the type of the pointer (which gives a null pointer value), and the two pointers are compared as described below </li> -<li> if one operand is a pointer and the other is a pointer to void, the non-void pointer is <a href="conversion" title="c/language/conversion">converted</a> to the pointer to void and the two pointers are compared as described below </li> -<li> two pointers compare equal if any of the following is true: </li> -<ul> -<li> they are both null pointer values of their type </li> -<li> they are both pointers to the same object or function </li> -<li> one pointer is to a struct/union/array object and the other is to its first member/any member/first element </li> -<li> they are both pointing one past the last element of the same array </li> -<li> 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 </li> -</ul> -</ul> <p>(as with relational operators, pointers to objects that aren't elements of any array behave as pointers to elements of arrays of size 1)</p> -<h4 id="Notes"> Notes</h4> <p>Objects of struct type do not compare equal automatically, and comparing them with <code><a href="../string/byte/memcmp" title="c/string/byte/memcmp">memcmp</a></code> is not reliable because the padding bytes may have any values.</p> -<p>Because pointer comparison works with pointers to void, the macro <code><a href="../types/null" title="c/types/NULL">NULL</a></code> may be defined as <code>(void*)0</code> in C, although that would be invalid in C++ where void pointers do not implicitly convert to typed pointers</p> -<p>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.</p> -<div class="t-example"> <div class="c source-c"><pre data-language="c">#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 -}</pre></div> </div> <h3 id="References"> References</h3> <ul> -<li> C17 standard (ISO/IEC 9899:2018): </li> -<ul> -<li> 6.5.8 Relational operators (p: 68-69) </li> -<li> 6.5.9 Equality operators (p: 69-70) </li> -</ul> -<li> C11 standard (ISO/IEC 9899:2011): </li> -<ul> -<li> 6.5.8 Relational operators (p: 95-96) </li> -<li> 6.5.9 Equality operators (p: 96-97) </li> -</ul> -<li> C99 standard (ISO/IEC 9899:1999): </li> -<ul> -<li> 6.5.8 Relational operators (p: 85-86) </li> -<li> 6.5.9 Equality operators (p: 86-87) </li> -</ul> -<li> C89/C90 standard (ISO/IEC 9899:1990): </li> -<ul> -<li> 3.3.8 Relational operators </li> -<li> 3.3.9 Equality operators </li> -</ul> -</ul> <h3 id="See_also"> See also</h3> <p><a href="operator_precedence" title="c/language/operator precedence"> Operator precedence</a></p> -<table class="wikitable"> <tr style="text-align:center"> <th colspan="7"> Common operators </th> -</tr> <tr style="text-align:center"> <td> <a href="operator_assignment" title="c/language/operator assignment"> assignment</a> </td> <td> <a href="operator_incdec" title="c/language/operator incdec"> increment<br>decrement</a> </td> <td> <a href="operator_arithmetic" title="c/language/operator arithmetic"> arithmetic</a> </td> <td> <a href="operator_logical" title="c/language/operator logical"> logical</a> </td> <td> <strong class="selflink"> comparison</strong> </td> <td> <a href="operator_member_access" title="c/language/operator member access"> member<br>access</a> </td> <td> <a href="operator_other" title="c/language/operator other"> other</a> </td> -</tr> <tr style="text-align:center"> <td> <p><code>a = b a += b a -= b a *= b a /= b a %= b a &= b a |= b a ^= b a <<= b a >>= b</code></p> -</td> <td> <p><code>++a --a a++ a--</code></p> -</td> <td> <p><code>+a -a a + b a - b a * b a / b a % b ~a a & b a | b a ^ b a << b a >> b</code></p> -</td> <td> <p><code>!a a && b a || b</code></p> -</td> <td> <p><code>a == b a != b a < b a > b a <= b a >= b</code></p> -</td> <td> <p><code>a[b] *a &a a->b a.b</code></p> -</td> <td> <p><code>a(...) a, b (type) a a ? b : c sizeof</code><br><br> <code>_Alignof</code><br><span class="t-mark-rev t-since-c11">(since C11)</span></p> -</td> -</tr> </table> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/operator_comparison" title="cpp/language/operator comparison">C++ documentation</a></span> for <span class=""><span>Comparison operators</span></span> </td> -</tr> </table> <div class="_attribution"> - <p class="_attribution-p"> - © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> - <a href="https://en.cppreference.com/w/c/language/operator_comparison" class="_attribution-link">https://en.cppreference.com/w/c/language/operator_comparison</a> - </p> -</div> |
