strcmp、wcscmp、_mbscmp
文字列を比較します。
重要
_mbscmp は、Windows ランタイム で実行するアプリケーションでは使用できません。詳細については、「/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' の前にあります。
文字セットと辞書式文字順序が異なるロケールでは、文字列の辞書式比較をするために strcmp の代わりに strcoll を使用できます。 あるいは、元の文字列に対して strxfrm を使用して、結果の文字列に対して strcmp を使用できます。
strcmp 関数では、大文字と小文字が区別されます。 _stricmp、_wcsicmp、および _mbsicmp は、文字列を最初に小文字の形式に変換してから比較します。 ASCII の表の 'Z' と 'a' の間にある文字 ('['、'\'、']'、'^'、'_'、および '`') を含む 2 つの文字列は、大文字と小文字によって異なる方法で比較されます。 たとえば、"ABCDE" と "ABCD^" の 2 つの文字列を比較する場合、小文字で比較する場合 ("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
参照
関連項目
_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