strcmp, wcscmp, _mbscmp, _mbscmp_l

文字列を比較する。

重要

_mbscmp および _mbscmp_l は、Windows ランタイムで実行するアプリケーションでは使用できません。 詳細については、「ユニバーサル 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 に対する、序数に基づく関係を示します。

Value string1string2 との関係
< 0 string1string2 より小さい
0 string1string2 と同じ
> 0 string1string2 より大きくなっています

パラメーター検証エラーが発生した場合、_mbscmp および _mbscmp_l_NLSCMPERROR を返します。これは、<string.h><mbstring.h> で定義されています。

解説

strcmp 関数は、string1string2 で序数に基づく比較を実行して、その関係を示す値を返します。 wcscmp 関数と _mbscmp 関数は、それぞれ、strcmp 関数のワイド文字バージョンとマルチバイト文字バージョンです。 _mbscmp は現在のマルチバイト コード ページに基づいてマルチバイト文字のシーケンスを認識し、エラーが発生した場合は _NLSCMPERROR を返します _mbscmp_l の動作は同じですが、現在のロケールではなく渡されるロケール パラメーターを使用します。 詳細については、「コード ページ」を参照してください。 また、null ポインターのstring2場合string1は、「_mbscmpパラメーターの検証」で説明されているように、無効なパラメーター ハンドラーを呼び出します。 実行の継続が許可された場合、_mbscmp および _mbscmp_l_NLSCMPERROR を返し、errnoEINVAL に設定します。 strcmp パラメーター wcscmp を検証しません。 それ以外では、これらの関数の動作は同じです。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

汎用テキスト ルーチンのマップ

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tcscmp strcmp _mbscmp wcscmp

関数はstrcmp、比較が序数であり、ロケールの影響を受けないというstrcmp点で関数とはstrcoll異なります。 strcoll は現在のロケールの LC_COLLATE カテゴリを使用して、文字列を辞書順で比較します。 LC_COLLATE カテゴリの詳細については、「setlocale_wsetlocale」を参照してください。

"C"ロケールでは、文字セット (ASCII 文字セット) 内の文字の順序は、辞書式文字順序と同じです。 ただし、その他のロケールでは、文字セット内の文字の順序が辞書式順序と異なる場合があります。 たとえば、ヨーロッパの一部のロケールでは、文字 "a" (値 0x61) は文字セットで文字 "ä" (値 0xE4) の前にありますが、辞書式の順序では文字 "ä" が文字 "a" の前にあります。

文字セットと辞書式文字順序が異なるロケールでは、文字列の辞書式比較をするために strcoll の代わりに strcmp を使用できます。 あるいは、元の文字列に対して 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>

互換性の詳細については、「 Compatibility」を参照してください。

ライブラリ

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