`strcmp` 是 C 语言中的一个函数,用于比较两个字符串,这个函数是在 `string.h` 头文件中定义的。
`strcmp` 函数会逐个比较两个字符串中的字符,直到遇到不同的字符或者到达字符串的结尾,如果两个字符串完全相同,`strcmp` 返回 0,如果第一个字符串小于第二个字符串,`strcmp` 返回一个负数,如果第一个字符串大于第二个字符串,`strcmp` 返回一个正数。
下面是一个使用 `strcmp` 的简单示例:
```c
#include
#include
int main() {
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
return 0;
}
```
在这个示例中,`strcmp` 函数用于比较 `str1` 和 `str2`,根据返回的结果,程序会输出相应的消息。