Javatpoint Logo
Javatpoint Logo

strcoll() in C++

Before diving into 'strcoll()' in C++, it's essential to understand the broader context of string comparison and the challenges it poses due to different character encodings and locale-specific rules. Let's explore these concepts and then delve into 'strcoll()' specifics.

String Comparison in C++:

In C++, strings are typically represented as arrays of characters that end with the null character (''\0''). Comparing strings involves comparing each character sequentially until a difference is encountered or the null character is reached. However, this straightforward approach may not be suitable for all scenarios.

Character Encodings:

Character encodings, such as ASCII, UTF-8, and UTF-16, represent characters differently. A naive comparison may lead to incorrect results when dealing with multibyte characters or characters outside the ASCII range. Therefore, specialized functions are required to handle these cases correctly.

Locale-specific Rules:

String comparison may also be influenced by locale-specific rules, where different languages and regions may have distinct character sorting orders. For example, the default sorting order in English is often case-sensitive, while some languages may have case-insensitive sorting.

The 'strcoll()' Function:

The 'strcoll()' function is part of the C and C++ standard libraries designed to address the abovementioned issues. It is used explicitly for string comparison with locale-specific collation.


It has the following syntax:


  • 'str1': Pointer to the first null-terminated byte string.
  • 'str2': Pointer to the second null-terminated byte string.

Return Value:

If 'str1' is less than, equal to, or greater than 'str2', an integer less than, equal to, or greater than zero is returned.

Understanding 'strcoll()':

1. Locale Awareness:

The strcoll() function considers the locale set by the 'setlocale()' function. The locale determines language-specific rules for string comparison.

2. Collation Order:

Unlike simple character comparison, 'strcoll()' uses collation order, which may differ from the ASCII or Unicode code point order. Collation considers language-specific rules for sorting characters.

3. Wide Character Version:

For wide character strings ('wchar_t'), the wide character version of 'strcoll()' is available as 'wcscoll()'.

4. Return Values:

The return value is an integer indicating the relationship between the two strings. A value less than, equal to, or greater than zero signifies that the first string is less than, equal to, or greater than the second string.

Example 1:

Let's take a C++ program to illustrate the strcoll() function:


str1 is lesser than str2

Example 2:

Let's take another C++ program to illustrate the strcoll() function:


str1 is greater than str2

Example 3:


str1 is equal to str2

Let's look deeper into the complexities of string comparison in C++ and the role of the strcoll() function in ensuring accurate and culturally sensitive comparisons. We'll look at the difficulties of multibyte character encodings, the importance of locale settings, and practical examples of how to use strcoll() in real-world scenarios.

Multibyte Character Encodings:

One of the challenges in string comparison arises from using multibyte character encodings, such as UTF-8 and UTF-16. These encodings represent a broader range of characters, including those outside the ASCII range. However, simple character-by-character comparisons may lead to incorrect results due to the variable length of multibyte characters.

The strcoll() function addresses this challenge by considering the entire character sequence, understanding the boundaries of multibyte characters, and comparing them appropriately. It ensures that strings containing multibyte characters are compared accurately, providing reliable results in scenarios where basic comparison methods might fail.

Locale Sensitivity:

Locale sensitivity is a fundamental aspect of string comparison. Different languages and regions have distinct rules for sorting characters, handling case sensitivity, and interpreting special characters. The strcoll() function considers the locale setting, allowing developers to tailor string comparisons to the linguistic and cultural context of the user.

Using setlocale(), developers can dynamically adjust the locale, enabling applications to adapt to users' preferences in different regions. This flexibility is crucial for creating software that respects the diversity of linguistic conventions and ensures a consistent and expected user experience.

Real-world Scenario: Sorting and Searching in Databases:

Consider a scenario where a database contains names of individuals from various countries. If a user needs a list of names sorted in a culturally appropriate order, strcoll() is helpful. Without proper locale-sensitive sorting, names may appear in an order that does not align with the user's expectations.


Sorted Names:


In this example, strcoll() is used in conjunction with std::qsort() to sort an array of names based on the English (United States) locale. The result is a culturally appropriate sorting order that respects the specific collation rules associated with that locale.

Wide Character Support:

The strcoll() function is not limited to narrow character strings. For wide-character strings (wchar_t), the wide-character version of strcoll() is available as scroll(). It is particularly relevant when dealing with applications that use wide characters, such as those targeting internationalization and supporting languages with complex character sets.

Handling Case Sensitivity:

Locale-aware string comparison often involves consideration of case sensitivity. Some languages have case-insensitive sorting rules, and strcoll() adapts to these rules based on the specified locale. It ensures that strings like "apple" and "Apple" are considered equivalent or are sorted in a manner consistent with the linguistic expectations of the user.

Performance Considerations:

While strcoll() is influential in providing locale-aware string comparison, it's essential to be mindful of performance considerations, especially in scenarios where string comparison is performed on large datasets. Depending on the complexity of the collation rules associated with a specific locale, there might be a performance overhead compared to more straightforward, non-locale-aware comparison functions.

Developers should weigh the benefits of accurate locale-sensitive comparisons against performance considerations and choose the appropriate string comparison method based on the specific requirements of their applications.

Integration with Standard Template Library (STL):

For developers working with the Standard Template Library (STL) in C++, incorporating strcoll() into algorithms like std::sort() provides a seamless way to achieve locale-sensitive sorting.

Here is an equivalent C++ version of the real-world scenario, using strcoll() for sorting a database of names:


Sorted Words:


In this example, the strcoll() function is seamlessly integrated with std::sort(), allowing for easy locale-aware sorting of a vector of strings. The result is a sorted list that aligns with the collation rules of the specified French (France) locale.


The strcoll() function in C++ is a robust tool for developers seeking to implement accurate and culturally sensitive string comparisons in their applications. By addressing the challenges of multibyte character encodings and considering locale-specific collation rules, strcoll() ensures that string comparisons align with user expectations across different languages and regions.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA