🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# C `strcoll()`函数 > 原文: [https://beginnersbook.com/2017/11/c-strcoll-function/](https://beginnersbook.com/2017/11/c-strcoll-function/) **`strcoll()`函数**类似于[`strcmp()`函数](https://beginnersbook.com/2017/11/c-strcmp-function/),它比较两个字符串并根据比较结果返回一个整数。 ## C `strcoll()`声明 ```c int strcoll(const char *str1, const char *str2) ``` `str1` - 第一个字符串 `str2` - 第二个字符串 ## `strcoll()`的返回值 * `> 0`:如果字符串`str1`中第一个不匹配字符的 ASCII 值大于`str2`。 * `< 0`:如果字符串`str1`中第一个不匹配字符的 ASCII 值小于`str2`。 * `= 0`:如果两个字符串相等 ## C `strcoll()`函数示例 ```c #include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; int result; strcpy(str1, "HELLO"); strcpy(str2, "hello world!"); result = strcoll(str1, str2); if(result > 0) { printf("ASCII value of first unmatched character of str1 is greater than str2"); } else if(result < 0) { printf("ASCII value of first unmatched character of str1 is less than str2"); } else { printf("Both the strings str1 and str2 are equal"); } return 0; } ``` 输出: ```c ASCII value of first unmatched character of str1 is less than str2 ``` 在这个例子中,我们比较两个不同的字符串。`strcoll()`函数区分大小写。 `'H'`的 ASCII 值小于`'h'`,这就是此函数返回负值的原因。 如果我们反转`strcoll()`函数中的参数,让我们说`strcoll(str2, str1)`,那么函数将返回一个正值。 #### 相关文章: 1. [C - `strcat()`函数](https://beginnersbook.com/2017/11/c-strcat-function-with-example/) 2. [C - `strchr()`函数](https://beginnersbook.com/2017/11/c-strchr-function/) 3. [C - `strncat()`函数](https://beginnersbook.com/2017/11/c-strncat-function/) 4. [C - `strncmp()`函数](https://beginnersbook.com/2017/11/c-strncmp-function/)