strcmp() – C语言库函数
C库函数 int strcmp(const char *str1, const char *str2) 比较字符串str1 指向 字符串str2。
声明
以下是声明的strcmp() 函数。
int strcmp(constchar*str1,constchar*str2)
参数
-
str1 -- 这是第一个要比较的字符串。
-
str2 -- 这是第二个的字符串进行比较。
返回值
这个函数的返回值如下:
-
如果返回值<0,则表明str1小于str2
-
如果返回值,如果> 0,则表明str2 小于 str1
-
如果返回值= 0,则表明str1 等于str2
例子
下面的例子显示了strncmp() 函数的用法。
#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 is less than str2");}elseif(ret <0){ printf("str2 is less than str1");}else{ printf("str1 is equal to str2");}return(0);}
让我们编译和运行上面的程序,这将产生以下结果:
str1 is less than str2