Share via


strcmp、wcscmp、_mbscmp

比较字符串。

重要

_mbscmp 无法用于在 Windows 运行时 中执行的应用程序。有关更多信息,请参见 CRT 函数不支持使用 /ZW

int strcmp(
   const char *string1,
   const char *string2 
);
int wcscmp(
   const wchar_t *string1,
   const wchar_t *string2 
);
int _mbscmp(
   const unsigned char *string1,
   const unsigned char *string2 
);

参数

  • string1, string2
    要比较的 null 终止的字符串。

返回值

这些函数的返回值指明 string1 和 string2 的序号关系。

string1 与 string2 的关系

< 0

string1 小于string2

0

string1 等同于string2

> 0

string1 大于string2

参数验证错误时,_mbscmp 返回 _NLSCMPERROR,该返回值是在 <string.h> 和 <mbstring.h> 中定义的。

备注

strcmp 函数对 string1 和 string2 执行序号比较并返回一个指示它们关系的值。 wcscmp 和 _mbscmp 分别是 strcmp 的宽字符和多字节字符版本。 _mbscmp 根据当前的多字节代码页识别多字节字符序列,并在发生错误时返回 _NLSCMPERROR。 有关详细信息,请参阅代码页。 同样,如果 string1 或 string2 为 null 指针,则 _mbscmp 将调用无效参数处理程序,如参数验证中所述。 如果允许继续执行,则 _mbscmp 返回 _NLSCMPERROR,并将 errno 设置为 EINVAL。 strcmp 和 wcscmp 不会验证其参数。 否则这三个函数否则具有相同行为。

一般文本例程映射

TCHAR.H 例程

未定义 _UNICODE 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tcscmp

strcmp

_mbscmp

wcscmp

strcmp 函数与 strcoll 的不同之处在于 strcmp 比较是按顺序进行的且不受区域设置影响。 strcoll 通过使用当前区域设置的 LC_COLLATE 类别以字典顺序来比较字符串。 有关 LC_COLLATE 类别的详细信息,请参阅 setlocale、_wsetlocale

在“C”区域设置中,字符集 (ASCII 字符集) 中的字符顺序与字典中的字符顺序一样。 但是在其他区域设置中,字符集中的字符顺序可能与字典中的顺序不同。 例如,在某些欧洲区域设置中,字符集中的字符“a”(值 0x61)位于字符“ä”(值 0xE4)之前,但在字典顺序中,字符“ä”位于字符“a”之前。

在字符集和字典字符顺序不同的区域设置中,可以使用 strcoll(而非 strcmp)来对字符串进行字典顺序的比较。 或者,也可以对原始字符串使用 strxfrm,然后对结果字符串使用 strcmp。

strcmp 函数区分大小写。 _stricmp、_wcsicmp 和 _mbsicmp 在比较字符串之前会首先将它们转换成小写形式。 包含位于 ASCII 表中“Z”和“a”之间的两个字符串(“[”、“\”、“]”、“^”、“_”和“`”)将有不同的比较结果,具体取决于它们的大小写形式。 例如,如果比较采用小写形式,则 "ABCDE" 和 "ABCD^" 这两个字符串件将以一种方式进行比较 ("abcde" > "abcd^"),而如果比较采用大写形式,则这两个字符串将采用另一种方式进行比较 ("ABCDE" < "ABCD^")。

要求

例程

必需的标头

strcmp

<string.h>

wcscmp

<string.h> 或 <wchar.h>

_mbscmp

<mbstring.h>

有关其他兼容性信息,请参阅兼容性

C 运行时库的所有版本。

示例

// crt_strcmp.c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

int main( void )
{
   char tmp[20];
   int result;

   // Case sensitive
   printf( "Compare strings:\n   %s\n   %s\n\n", string1, string2 );
   result = strcmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof (tmp), "less than" );
   else
      strcpy_s( tmp, _countof (tmp), "equal to" );
   printf( "   strcmp:   String 1 is %s string 2\n", tmp );

   // Case insensitive (could use equivalent _stricmp)
   result = _stricmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof (tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof (tmp), "less than" );
   else
      strcpy_s( tmp, _countof (tmp), "equal to" );
   printf( "   _stricmp:  String 1 is %s string 2\n", tmp );
}
  

.NET Framework 等效项

System::String::CompareOrdinal

请参见

参考

字符串操作 (CRT)

memcmp、wmemcmp

_memicmp、_memicmp_l

strcoll 函数

_stricmp、_wcsicmp、_mbsicmp、_stricmp_l、_wcsicmp_l、_mbsicmp_l

strncmp、wcsncmp、_mbsncmp、_mbsncmp_l

_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l

strrchr、wcsrchr、_mbsrchr、_mbsrchr_l

strspn、wcsspn、_mbsspn、_mbsspn_l

strxfrm、wcsxfrm、_strxfrm_l、_wcsxfrm_l