In C, strcoll() compares two C-strings using the current locale. Alphabet order and accents can follow the language, instead of raw character codes.
You pass both strings. Both are required. The result uses the same 0 / less than 0 / greater than 0 idea as strcmp(). Call setlocale first for a specific language. Include <string.h>.
Syntax
Pass both strings after setting the locale. Start with this call.
C
strcoll(a, b);
Call
What it does
strcoll(a, b)
Compares a and b with the current LC_COLLATE rules.
Include <string.h> and <locale.h>. Locale names differ by operating system.
Library declarations
C
int strcoll(const char *s1, const char *s2);
Parameters
Parameter
Description
first
Required. The first C-string.
second
Required. The second C-string.
Return Value
Return Value
Description
0
The strings compare as equal under the locale.
Less than 0
The first string comes first under the locale.
Greater than 0
The first string comes after under the locale.
C Examples
Example 1 — Compare After Setting Locale
Call setlocale once, then use strcoll like strcmp for equality and order.
C
#include <stdio.h>
#include <string.h>
#include <locale.h>
int main(void) {
setlocale(LC_COLLATE, "");
if (strcoll("apple", "apple") == 0) {
printf("equal under locale\n");
}
return 0;
}
Output
equal under locale
How It Works
Include locale.h. That declares setlocale and LC_COLLATE.
Enable collation.setlocale(LC_COLLATE, "") picks the environment locale.
Compare with strcoll. Zero means equivalent under those rules.
Same test style as strcmp. Still use == 0 for equality.
Example 2 — Show When You Want Locale Order
Use strcmp for exact tokens; use strcoll when presenting a sorted list to people.