C strcoll() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

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);
CallWhat 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

ParameterDescription
firstRequired. The first C-string.
secondRequired. The second C-string.

Return Value

Return ValueDescription
0The strings compare as equal under the locale.
Less than 0The first string comes first under the locale.
Greater than 0The 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;
}

How It Works

  1. Include locale.h. That declares setlocale and LC_COLLATE.
  2. Enable collation. setlocale(LC_COLLATE, "") picks the environment locale.
  3. Compare with strcoll. Zero means equivalent under those rules.
  4. 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.

C
#include <stdio.h>
#include <string.h>
#include <locale.h>

int main(void) {
    const char *a = "Apple";
    const char *b = "banana";

    setlocale(LC_COLLATE, "");

    printf("strcmp  sign: %s\n",
           (strcmp(a, b) < 0) ? "<0" : ">0");
    printf("strcoll sign: %s\n",
           (strcoll(a, b) < 0) ? "<0" : ">0");

    return 0;
}

How It Works

  1. Two tools, two jobs. Print both signs side by side.
  2. strcmp uses bytes. 'A' vs 'b' follows raw values.
  3. strcoll uses locale. Many locales still put Apple before banana here.
  4. Results can diverge. With accented text or other locales, order may differ from strcmp.

Example 3 — Pick Which Name Comes First

Branch on the sign to decide display order for two user-facing strings.

C
#include <stdio.h>
#include <string.h>
#include <locale.h>

int main(void) {
    int r;

    setlocale(LC_COLLATE, "");
    r = strcoll("Zoe", "Amy");

    if (r < 0) {
        printf("Zoe comes first\n");
    } else if (r > 0) {
        printf("Amy comes first\n");
    } else {
        printf("same under locale\n");
    }

    return 0;
}

How It Works

  1. Set collation first. Without it, you often get C-locale byte order.
  2. Compare the names. strcoll("Zoe", "Amy") is positive in typical locales.
  3. Branch on the sign. r > 0 means Amy sorts earlier.
  4. Do not require ±1. Only < 0, == 0, and > 0 are portable.

Frequently Asked Questions

strcoll() compares two C-strings using the current locale rules.
Both are required. first and second are the C-strings to compare.
0 means equal under the locale. The sign tells which string comes first.
Include <string.h>. Also include <locale.h> when you call setlocale.
Use strcmp() for exact byte matching, such as passwords or protocol tokens.

Next: strspn()

Measure how many leading characters stay inside a allowed set.

strspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful