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");
Call
What 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
Parameter
Description
first
Required. The first C-string.
second
Required. The second C-string.
Return Value
Return Value
Description
0
The strings are equal under the current language rules.
Less than 0
The first string comes before the second.
Greater than 0
The 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;
}
Output
equal
How It Works
strcoll(a, b) == 0 means equal under current rules.
For plain English ASCII, this often matches strcmp.
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;
}
Output
Apple before Banana
How It Works
setlocale(LC_COLLATE, "") selects the environment locale.
strcoll then uses that language’s sort rules.
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;
}