summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrcoll.html
blob: 8c19e000ba91a3c8f6111243ac9739b71f79a24e (plain)
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
59
60
    <h1 id="firstHeading" class="firstHeading">strcoll</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int strcoll( const char* lhs, const char* rhs );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Compares two null-terminated byte strings according to the current locale as defined by the <code><a href="../../locale/lc_categories" title="c/locale/LC categories">LC_COLLATE</a></code> category.</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 null-terminated byte strings to compare </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <ul>
<li> Negative value if <code>lhs</code> is <i>less than</i> (precedes) <code>rhs</code>. </li>
<li> <code>​0​</code> if <code>lhs</code> is <i>equal to</i> <code>rhs</code>. </li>
<li> Positive value if <code>lhs</code> is <i>greater than</i> (follows) <code>rhs</code>. </li>
</ul> <h3 id="Notes"> Notes</h3> <p>Collation order is the dictionary order: the position of the letter in the national alphabet (its <i>equivalence class</i>) has higher priority than its case or variant. Within an equivalence class, lowercase characters collate before their uppercase equivalents and locale-specific order may apply to the characters with diacritics. In some locales, groups of characters compare as single <i>collation units</i>. For example, <code>"ch"</code> in Czech follows <code>"h"</code> and precedes <code>"i"</code>, and <code>"dzs"</code> in Hungarian follows <code>"dz"</code> and precedes <code>"g"</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;locale.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
 
int main(void)
{
    setlocale(LC_COLLATE, "cs_CZ.utf8");
    // Alternatively, ISO-8859-2 (a.k.a. Latin-2)
    // may also work on some OS:
    // setlocale(LC_COLLATE, "cs_CZ.iso88592");
 
    const char* s1 = "hrnec";
    const char* s2 = "chrt";
 
    printf("In the Czech locale: ");
    if (strcoll(s1, s2) &lt; 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
 
    printf("In lexicographical comparison: ");
    if (strcmp(s1, s2) &lt; 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.24.4.3 The strcoll function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.24.4.3 The strcoll function (p: TBD) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.24.4.3 The strcoll function (p: 366) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.4.3 The strcoll function (p: 329) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.4.3 The strcoll function </li></ul>
</ul>                   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../wide/wcscoll" title="c/string/wide/wcscoll"> <span class="t-lines"><span>wcscoll</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 two wide strings in accordance to the current locale <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strxfrm" title="c/string/byte/strxfrm"> <span class="t-lines"><span>strxfrm</span></span></a></div> </td> <td> transform a string so that strcmp would produce the same result as strcoll <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcsxfrm" title="c/string/wide/wcsxfrm"> <span class="t-lines"><span>wcsxfrm</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> transform a wide string so that wcscmp would produce the same result as wcscoll <br> <span class="t-mark">(function)</span>  </td>
</tr> <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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strcoll" title="cpp/string/byte/strcoll">C++ documentation</a></span> for <code>strcoll</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/strcoll" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strcoll</a>
  </p>
</div>