C++ strcoll() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strcoll() compares two C-strings using the sorting rules of the current language, called the locale. Unlike strcmp(), it does not use raw character codes, so accents and alphabet order can follow that language.

Both strings are required. The result is 0 when they are equal under those rules, less than 0 when the first comes first, and greater than 0 when it comes after. Call setlocale first if you need a specific language. Include <cstring>.

Syntax

Compare the two strings after the locale is set.

C++
strcoll("apple", "banana");
CallWhat it does
strcoll("apple", "banana")Compares the words with the current language rules. apple comes first, so the result is less than 0.

Set the locale with setlocale before you rely on language order. Include <clocale> for that call.

Library declarations
C++
int strcoll(const char* lhs, const char* rhs);

Parameters

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

Return Value

Return ValueDescription
0The strings are equal under the current language rules.
Less than 0The first string comes before the second.
Greater than 0The first string comes after the second.

C++ Examples

Example 1 — Basic Equality

Identical ASCII text compares equal, just like strcmp.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* a = "CodeToFun";
    const char* b = "CodeToFun";

    if (strcoll(a, b) == 0) {
        std::cout << "Equal\n";
    }
    return 0;
}

How It Works

  1. strcoll(a, b) == 0 means equal under current rules.
  2. For plain English ASCII, this often matches strcmp.
  3. Locale matters more for accents and special letters.

Example 2 — Choose a Locale

Ask the environment for the user’s collation locale.

C++
#include <iostream>
#include <cstring>
#include <clocale>

int main() {
    setlocale(LC_COLLATE, "");

    const char* left  = "Apple";
    const char* right = "Banana";
    int result = strcoll(left, right);

    if (result < 0) {
        std::cout << left << " before " << right << '\n';
    } else if (result > 0) {
        std::cout << left << " after " << right << '\n';
    } else {
        std::cout << "equal\n";
    }
    return 0;
}

How It Works

  1. setlocale(LC_COLLATE, "") selects the environment locale.
  2. strcoll then uses that language’s sort rules.
  3. Sign of the result means before / after / equal.

Example 3 — strcoll vs strcmp

Same call shape; different rules when the locale is set.

C++
#include <iostream>
#include <cstring>
#include <clocale>

int main() {
    setlocale(LC_COLLATE, "");

    const char* a = "resume";
    const char* b = "Resume";

    std::cout << "strcmp:  " << strcmp(a, b) << '\n';
    std::cout << "strcoll: " << strcoll(a, b) << '\n';
    return 0;
}

How It Works

  1. strcmp compares code units—case always differs here.
  2. strcoll may treat case differently depending on locale.
  3. Exact numbers vary by OS; trust the sign (or == 0).

Frequently Asked Questions

strcoll() compares two C-strings using the sorting rules of the current locale.
Two C-strings. The first is compared with the second.
0 when they are equal under those rules, a negative number when the first comes first, and a positive number when the first comes after.
strcmp() uses character codes. strcoll() uses the language rules from the current locale.
Yes, if you want a specific language order. Call setlocale before strcoll().

Next: strcpy()

Copy one C-string into a destination buffer.

strcpy() 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.

8 people found this page helpful