目录

int strcmp(const char *str1, const char *str2)

描述 (Description)

C库函数int strcmp(const char *str1, const char *str2)str1的字符串与str2 str1的字符串进行比较。

声明 (Declaration)

以下是strcmp()函数的声明。

int strcmp(const char *str1, const char *str2)

参数 (Parameters)

  • str1 - 这是要比较的第一个字符串。

  • str2 - 这是要比较的第二个字符串。

返回值 (Return Value)

此函数返回如下值 -

  • 如果返回值<0则表示str1小于str2。

  • 如果返回值> 0则表示str2小于str1。

  • 如果返回值= 0则表示str1等于str2。

例子 (Example)

以下示例显示了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");
   } else if(ret > 0) {
      printf("str2 is less than str1");
   } else {
      printf("str1 is equal to str2");
   }
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果 -

str2 is less than str1
↑回到顶部↑
WIKI教程 @2018