共用方式為


memcmp, wmemcmp

比較兩個緩衝區中的字元。

語法

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
第二個緩衝區。

count
要比較的字元數。 (比較 memcmp 和寬字元 wmemcmp 的位元組數)。

傳回值

傳回值表示緩衝區之間的關聯性。

傳回值 和的第一 count 個字元 buf1 關聯性 buf2
< 0 buffer1 小於 buffer2
0 buffer1 等於 buffer2
> 0 buffer1 大於 buffer2

備註

比較 buffer1buffer2 的前 count 個字元,並傳回指出其關聯性的值。 非零傳回值的正負號是緩衝區中第一組不同值之間差異的正負號。 值會解譯為 memcmpunsigned char,以及 wmemcmpwchar_t

需求

常式 必要的標頭
memcmp <memory.h><string.h>
wmemcmp <wchar.h>

如需相容性詳細資訊,請參閱相容性

程式庫

所有版本的 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