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
要使用的地區設定。
傳回值
每一個這些函式的傳回值均表示 string1 與 string2 的序數關聯。
| 值 | 的 string1 關聯性 string2 |
|---|---|
| < 0 | string1 小於 string2 |
| 0 | string1 等於 string2 |
| > 0 | string1 大於 string2 |
在和 <mbstring.h> 中 <string.h> 定義的參數驗證錯誤 _mbscmp 和 _mbscmp_l_NLSCMPERROR 傳回。
備註
strcmp 函式會執行 string1 和 string2 的序數比較,並傳回指出其關聯性的值。 wcscmp 和 _mbscmp 分別是 strcmp 的寬字元版本和多位元組字元版本。 _mbscmp 根據目前的多位元組字碼頁來辨識多位元組字元序列,並在發生錯誤時傳回 _NLSCMPERROR。 _mbscmp_l 具有相同的行為,但會使用傳入的地區設定參數,而不是目前的地區設定。 如需詳細資訊,請參閱字碼頁。 此外,如果 string1 或 string2 為 null 指標,_mbscmp 會叫用無效的參數處理常式,如參數驗證中所述。 如果允許繼續執行,則 _mbscmp 和 _mbscmp_l 會傳回 _NLSCMPERROR,且 errno 設為 EINVAL。 strcmp 和 wcscmp 不會驗證其參數。 除此之外,這些函式的行為相同。
依預設,此函式的全域狀態範圍為應用程式。 若要變更此項,請參閱 CRT 中的全域狀態。
一般文字常式對應
TCHAR.H 常規 |
_UNICODE&_MBCS 未定義 |
_MBCS 定義 |
_UNICODE 定義 |
|---|---|---|---|
_tcscmp |
strcmp |
_mbscmp |
wcscmp |
strcmp 函式與 strcoll 函式不同之處為,strcmp 的比較為序數,且不會受到地區設定影響。 strcoll 使用目前地區設定的 LC_COLLATE 分類,以詞典編纂順序比較字串。 如需類別的詳細資訊,請參閱 setlocale_wsetlocale 。LC_COLLATE
在 "C" 地區設定中,字元集 (ASCII 字元集) 的字元順序與詞典編纂的字元順序相同。 不過,其他地區設定中,字元集的字元順序可能與詞典編纂順序不同。 例如,在特定的歐洲地區設定中,字元 ' a ' (值 0x61) 出現在字元集的字元 ' ä ' () 值之前,但字元 ' ä ' 會在字元 ' a ' 詞典編纂前面。
在字元集和詞典編纂字元順序不同的地區設定中,您可以針對字串的詞典編纂比較使用 strcoll,而不是 strcmp。 或者,您可以在原始字串使用 strxfrm,然後在產生的字串使用 strcmp。
strcmp 函式會區分大小寫。 _stricmp、_wcsicmp 和 _mbsicmp 會先將它們轉換成其小寫的形式來比較字串。 有兩個字串包含位於 ASCII 資料表中 ' Z ' 和 ' a ' 之間的字元 ( ' [ '、' \\ '、' ] '、' ^ '、' _ ' 和 ' ` ' ) 根據其大小寫不同。 例如,兩個字串 " ABCDE " 和 " ABCD^ " 相較于小寫 ( " >abcdeabcd^ " "" ) ,另一個方法則 ( " 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