C Function Strcoll
# C Library Function - strcoll()
The `strcoll()` function is a built-in function in the C standard library defined in the `` header file. It is used to compare two null-terminated strings based on the collation sequence defined by the program's current locale.
---
## Description
The `strcoll()` function compares the string pointed to by `str1` to the string pointed to by `str2`. Unlike `strcmp()`, which performs a raw byte-by-byte comparison based on ASCII values, `strcoll()` performs a comparison that is sensitive to the system's current locale setting, specifically the `LC_COLLATE` category.
This makes `strcoll()` highly useful for internationalization (i18n), as it correctly handles language-specific sorting rules (e.g., accents, ligatures, and alphabetical orders that differ from standard ASCII).
---
## Declaration
Below is the prototype for the `strcoll()` function:
```c
int strcoll(const char *str1, const char *str2);
```
### Parameters
* **`str1`** -- A pointer to the first null-terminated string to be compared.
* **`str2`** -- A pointer to the second null-terminated string to be compared.
### Return Value
The function returns an integer value indicating the relationship between the two strings:
| Return Value | Description |
| :--- | :--- |
| **`< 0`** (Negative value) | `str1` is lexicographically less than `str2` according to the current locale. |
| **`0`** | `str1` is equal to `str2`. |
| **`> 0`** (Positive value) | `str1` is lexicographically greater than `str2` according to the current locale. |
---
## Code Example
The following example demonstrates how to use `strcoll()` to compare two strings.
```c
#include
#include
int main() {
char str1;
char str2;
int ret;
// Initialize the strings
strcpy(str1, "abc");
strcpy(str2, "ABC");
// Compare strings using strcoll
ret = strcoll(str1, str2);
if (ret > 0) {
printf("str1 is greater than str2\n");
} else if (ret < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is equal to str2\n");
}
return 0;
}
```
### Output
When compiled and executed under the default `"C"` locale, the program will output:
```text
str1 is greater than str2
```
*(Note: In the default "C" locale, lowercase letters have higher ASCII values than uppercase letters, so `"abc"` is evaluated as greater than `"ABC"`).*
---
## Key Considerations
### 1. `strcoll()` vs. `strcmp()`
* **`strcmp()`** compares strings byte-by-byte using their numerical ASCII/binary values. It is fast and efficient but does not respect regional alphabetical rules.
* **`strcoll()`** interprets strings according to the rules of the current locale. For example, in Spanish, "ch" might be treated as a single letter sorting between "c" and "d", or accented characters like "Γ‘" might sort next to "a" rather than at the end of the ASCII table.
### 2. Setting the Locale
To make `strcoll()` behave according to a specific language or region, you must set the locale using the `setlocale()` function from ``.
```c
#include
#include
// Set the locale to the user's system default environment
setlocale(LC_COLLATE, "");
// Or set it to a specific locale, e.g., Spanish
setlocale(LC_COLLATE, "es_ES.UTF-8");
```
If no locale is explicitly set in your program, `strcoll()` defaults to the `"C"` locale, making its behavior identical to `strcmp()`.
### 3. Performance Note
Because `strcoll()` has to query locale-specific rules and transform strings internally to perform comparisons, it is computationally more expensive than `strcmp()`. If you need to sort a large array of strings repeatedly using locale rules, consider using `strxfrm()` to transform the strings first, and then use `strcmp()` on the transformed strings to improve performance.
YouTip