diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fbyte%2Fmemcmp.html | |
new repository
Diffstat (limited to 'devdocs/c/string%2Fbyte%2Fmemcmp.html')
| -rw-r--r-- | devdocs/c/string%2Fbyte%2Fmemcmp.html | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fbyte%2Fmemcmp.html b/devdocs/c/string%2Fbyte%2Fmemcmp.html new file mode 100644 index 00000000..4d8d372f --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fmemcmp.html @@ -0,0 +1,57 @@ + <h1 id="firstHeading" class="firstHeading">memcmp</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int memcmp( const void* lhs, const void* rhs, size_t count );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Compares the first <code>count</code> bytes of the objects pointed to by <code>lhs</code> and <code>rhs</code>. The comparison is done lexicographically.</p> +<p>The sign of the result is the sign of the difference between the values of the first pair of bytes (both interpreted as <code>unsigned char</code>) that differ in the objects being compared.</p> +<p>The behavior is undefined if access occurs beyond the end of either object pointed to by <code>lhs</code> and <code>rhs</code>. The behavior is undefined if either <code>lhs</code> or <code>rhs</code> is a null pointer.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> lhs, rhs </td> <td> - </td> <td> pointers to the objects to compare </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> number of bytes to examine </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>Negative value if <code>lhs</code> appears before <code>rhs</code> in lexicographical order.</p> +<p>Zero if <code>lhs</code> and <code>rhs</code> compare equal, or if count is zero.</p> +<p>Positive value if <code>lhs</code> appears after <code>rhs</code> in lexicographical order.</p> +<h3 id="Notes"> Notes</h3> <p>This function reads <a href="../../language/object" title="c/language/object">object representations</a>, not the object values, and is typically meaningful for byte arrays only: structs may have padding bytes whose values are indeterminate, the values of any bytes beyond the last stored member in a union are indeterminate, and a type may have two or more representations for the same value (different encodings for +0 and -0 or for +0.0 and –0.0, indeterminate padding bits within the type).</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <string.h> + +void demo(const char* lhs, const char* rhs, size_t sz) +{ + for(size_t n = 0; n < sz; ++n) + putchar(lhs[n]); + + int rc = memcmp(lhs, rhs, sz); + const char *rel = rc < 0 ? " precedes " : rc > 0 ? " follows " : " compares equal "; + fputs(rel, stdout); + + for(size_t n = 0; n < sz; ++n) + putchar(rhs[n]); + puts(" in lexicographical order"); +} + +int main(void) +{ + char a1[] = {'a','b','c'}; + char a2[sizeof a1] = {'a','b','d'}; + + demo(a1, a2, sizeof a1); + demo(a2, a1, sizeof a1); + demo(a1, a1, sizeof a1); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">abc precedes abd in lexicographical order +abd follows abc in lexicographical order +abc compares equal to abc in lexicographical order</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.24.4.1 The memcmp function (p: 266) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.24.4.1 The memcmp function (p: 365) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.21.4.1 The memcmp function (p: 328) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.11.4.1 The memcmp function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strcmp" title="c/string/byte/strcmp"> <span class="t-lines"><span>strcmp</span></span></a></div> </td> <td> compares two strings <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="strncmp" title="c/string/byte/strncmp"> <span class="t-lines"><span>strncmp</span></span></a></div> </td> <td> compares a certain amount of characters of two strings <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/memcmp" title="cpp/string/byte/memcmp">C++ documentation</a></span> for <code>memcmp</code> </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/string/byte/memcmp" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/memcmp</a> + </p> +</div> |
