1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<h1 id="firstHeading" class="firstHeading">strncmp</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 strncmp( const char* lhs, const char* rhs, size_t count );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Compares at most <code>count</code> characters of two possibly null-terminated arrays. The comparison is done lexicographically. Characters following the null character are not compared.</p>
<p>The sign of the result is the sign of the difference between the values of the first pair of characters (both interpreted as <code>unsigned char</code>) that differ in the arrays being compared.</p>
<p>The behavior is undefined when access occurs past the end of either array <code>lhs</code> or <code>rhs</code>. The behavior is undefined when either <code>lhs</code> or <code>rhs</code> is the 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 possibly null-terminated arrays to compare </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of characters to compare </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 is not locale-sensitive, unlike <code><a href="strcoll" title="c/string/byte/strcoll">strcoll</a></code> and <code><a href="strxfrm" title="c/string/byte/strxfrm">strxfrm</a></code>.</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, int sz)
{
const int rc = strncmp(lhs, rhs, sz);
if (rc < 0)
printf("First %d chars of [%s] precede [%s]\n", sz, lhs, rhs);
else if (rc > 0)
printf("First %d chars of [%s] follow [%s]\n", sz, lhs, rhs);
else
printf("First %d chars of [%s] equal [%s]\n", sz, lhs, rhs);
}
int main(void)
{
const char* string = "Hello World!";
demo(string, "Hello!", 5);
demo(string, "Hello", 10);
demo(string, "Hello there", 10);
demo("Hello, everybody!" + 12, "Hello, somebody!" + 11, 5);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">First 5 chars of [Hello World!] equal [Hello!]
First 10 chars of [Hello World!] follow [Hello]
First 10 chars of [Hello World!] precede [Hello there]
First 5 chars of [body!] equal [body!]</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.24.4.4 The strncmp function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.24.4.4 The strncmp function (p: TBD) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.24.4.4 The strncmp function (p: 366) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.4.4 The strncmp function (p: 329) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.4.4 The strncmp 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="../wide/wcsncmp" title="c/string/wide/wcsncmp"> <span class="t-lines"><span>wcsncmp</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> compares a certain amount of characters from two wide strings <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="memcmp" title="c/string/byte/memcmp"> <span class="t-lines"><span>memcmp</span></span></a></div> </td> <td> compares two buffers <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strcoll" title="c/string/byte/strcoll"> <span class="t-lines"><span>strcoll</span></span></a></div> </td> <td> compares two strings in accordance to the current locale <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/strncmp" title="cpp/string/byte/strncmp">C++ documentation</a></span> for <code>strncmp</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/strncmp" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strncmp</a>
</p>
</div>
|