Share via


memcmp、wmemcmp

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

int memcmp( 
   const void *buf1, 
   const void *buf2, 
   size_t count 
); 
int wmemcmp( 
   const wchar_t * buf1, 
   const wchar_t * buf2, 
   size_t count 
);

參數

  • buf1
    第一個緩衝區。

  • buf2
    第二個緩衝區。

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

傳回值

回傳表示兩個緩衝區大小關係的值。

傳回值

buf1 和 buf2第一個 count 字元之間的大小關係

< 0

buf1 小於 buf2

0

buf1 與 buf2 相同

> 0

buf1 大於 buf2

備註

比較 buf1 和 buf2 的第一個 count 字元並傳回表示其大小關係的值。 非零傳回值的正負號是在緩衝區中表示第一對不同的值之差異的標記。 其值會解譯為 memcmp的 unsigned char ,以及解釋為 wmemcmp的 wchar_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" );
}

Output

Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '1,2' to '1,2':
int_arr1 is equal to int_arr2.

.NET Framework 對等用法

不適用。若要呼叫標準 C 函式,請使用 PInvoke。如需詳細資訊,請參閱平台叫用範例

請參閱

參考

緩衝區操作

_memccpy

memchr、wmemchr

memcpy、wmemcpy

memset、wmemset

strcmp、wcscmp、_mbscmp

strncmp、wcsncmp、_mbsncmp、_mbsncmp_l