C 库函数 int strcmp(const char *str1, const char *str2) 把 str1 所指向的字符串和 str2 所指向的字符串进行比较。
下面是 strcmp() 函数的声明。
int strcmp(const char *str1, const char *str2)
该函数返回值如下:
下面的示例演示了 strcmp() 函数的用法。
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret < 0)
{
printf("str1 小于 str2");
}
else if(ret > 0)
{
printf("str1 大于 str2");
}
else
{
printf("str1 等于 str2");
}
return(0);
}让我们编译并运行上面的程序,这将产生以下结果:
str1 大于 str2