strcmp、 、 wcscmp_mbscmp_mbscmp_l

比較字串。

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbscmp_mbscmp_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 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
);
int _mbscmp_l(
   const unsigned char *string1,
   const unsigned char *string2,
   _locale_t locale
);

參數

string1, string2
以 Null 結束的待比較字串。

locale
要使用的地區設定。

傳回值

每一個這些函式的傳回值均表示 string1string2 的序數關聯。

ReplTest1 與的 string1 關聯性 string2
< 0 string1 小於 string2
0 string1 等於 string2
> 0 string1 大於 string2

在參數驗證錯誤上,_mbscmp_mbscmp_l傳回 _NLSCMPERROR,其定義於和<mbstring.h><string.h>

備註

strcmp 函式會執行 string1string2 的序數比較,並傳回指出其關聯性的值。 wcscmp_mbscmp 分別是 strcmp 的寬字元版本和多位元組字元版本。 _mbscmp 根據目前的多位元組字碼頁來辨識多位元組字元序列,並在發生錯誤時傳回 _NLSCMPERROR_mbscmp_l 具有相同的行為,但會使用傳入的地區設定參數,而不是目前的地區設定。 如需詳細資訊,請參閱 代碼頁。 此外,如果 string1string2 為 Null 指標,_mbscmp請叫用無效的參數處理程式,如參數驗證中所述。 如果允許繼續執行,則 _mbscmp_mbscmp_l 會傳回 _NLSCMPERROR,且 errno 設為 EINVALstrcmpwcscmp 不會驗證其參數。 除此之外,這些函式的行為相同。

根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態

一般文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 已定義 _UNICODE 已定義
_tcscmp strcmp _mbscmp wcscmp

strcmp 式與比較中的 strcoll 函式不同, strcmp 因為比較是序數,而且不會受到地區設定的影響。 strcoll 使用目前地區設定的 LC_COLLATE 分類,以詞典編纂順序比較字串。 如需類別的詳細資訊 LC_COLLATE ,請參閱 setlocale_wsetlocale

在 "C" 地區設定中,字元集 (ASCII 字元集) 的字元順序與詞典編纂的字元順序相同。 不過,其他地區設定中,字元集的字元順序可能與詞典編纂順序不同。 例如,在某些歐洲地區設定中,字元 'a' (值0x61) 位於字元集的字元 'ä' (value 0xE4)之前,但字元 'ä' 位於字元 'a' 語彙上。

在字元集和詞典編纂字元順序不同的地區設定中,您可以針對字串的詞典編纂比較使用 strcoll,而不是 strcmp。 或者,您可以在原始字串使用 strxfrm,然後在產生的字串使用 strcmp

strcmp 函式會區分大小寫。 _stricmp_wcsicmp_mbsicmp 會先將它們轉換成其小寫的形式來比較字串。 兩個字串,其中包含位於 ASCII 數據表中 'Z' 和 'a' 之間的字元('['、'、'\\、'^]、'、 ''_ 和 '`') 會根據大小寫而有所不同。 例如,如果比較是小寫 ,則兩個字符串 “ABCDE” 和 “ABCD^” 會比較一種方式(“” >abcd^abcde“”“”),如果比較是大寫,則以另一種方式 (“”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 );
}
Compare strings:
   The quick brown dog jumps over the lazy fox
   The QUICK brown dog jumps over the lazy fox

   strcmp:   String 1 is greater than string 2
   _stricmp:  String 1 is equal to string 2

另請參閱

字串操作
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