memcmp, wmemcmp

2 つのバッファーの文字を比較します。

構文

int memcmp(
   const void *buffer1,
   const void *buffer2,
   size_t count
);
int wmemcmp(
   const wchar_t * buffer1,
   const wchar_t * buffer2,
   size_t count
);

パラメーター

buffer1
最初のバッファー。

buffer2
2 番目のバッファー。

count
比較する文字数 (memcmp の場合はバイト数、wmemcmp の場合はワイド文字数を比較します)。

戻り値

戻り値は、バッファー間の関係を示しています。

戻り値 buf1buf2 の最初の count 文字数の関係
< 0 buffer1buffer2 より小さい
0 buffer1buffer2 と同じ
> 0 buffer1buffer2 より大きい

解説

countbuffer1 の最初の buffer2 文字数を比較します。 ゼロ以外の戻り値の符号は、バッファー間で最初に異なる値のペアの差の符号です。 値は、unsigned char の場合は memcmp として解釈され、wchar_t の場合は wmemcmp として解釈されます。

必要条件

ルーチンによって返される値 必須ヘッダー
memcmp <memory.h> または <string.h>
wmemcmp <wchar.h>

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

ライブラリ

C ランタイム ライブラリのすべてのバージョン。

// crt_memcmp.c
/* This program uses memcmp to compare
* the strings named first and second. If the first
* 19 bytes of the strings are equal, the program
* considers the strings to be equal.
*/

#include <string.h>
#include <stdio.h>

int main( void )
{
   char first[]  = "12345678901234567890";
   char second[] = "12345678901234567891";
   int int_arr1[] = {1,2,3,4};
   int int_arr2[] = {1,2,3,4};
   int result;

   printf( "Compare '%.19s' to '%.19s':\n", first, second );
   result = memcmp( first, second, 19 );
   if( result < 0 )
      printf( "First is less than second.\n" );
   else if( result == 0 )
      printf( "First is equal to second.\n" );
   else
      printf( "First is greater than second.\n" );

   printf( "Compare '%d,%d' to '%d,%d':\n", int_arr1[0], int_arr1[1], int_arr2[0], int_arr2[1]);
   result = memcmp( int_arr1, int_arr2, sizeof(int) * 2 );
   if( result < 0 )
      printf( "int_arr1 is less than int_arr2.\n" );
   else if( result == 0 )
      printf( "int_arr1 is equal to int_arr2.\n" );
   else
      printf( "int_arr1 is greater than int_arr2.\n" );
}
Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '1,2' to '1,2':
int_arr1 is equal to int_arr2.

関連項目

バッファー操作
_memccpy
memchr, wmemchr
memcpy, wmemcpy
memset, wmemset
strcmp, wcscmp, _mbscmp
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l