strcmp函数用于比较两个字符串的大小,返回值有三种情况:
以下是使用strcmp比较字符串大小的示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abc";
char str2[] = "def";
int result = strcmp(str1, str2);
if (result == 0) {
printf("str1 is equal to str2\n");
} else if (result > 0) {
printf("str1 is greater than str2\n");
} else {
printf("str1 is less than str2\n");
}
return 0;
}
输出结果为:
str1 is less than str2
在此示例中,字符串"abc"小于字符串"def",所以strcmp返回一个负数。