Share via


strcmp、wcscmp、_mbscmp

比較字串。

重要

不可在於 Windows 執行階段 中執行的應用程式中使用 _mbscmp。如需詳細資訊,請參閱 /ZW 不支援 CRT 函式

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