memcmp, wmemcmp

Confronta i caratteri in due buffer.

Sintassi

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

Parametri

buffer1
Primo buffer.

buffer2
Secondo buffer.

count
Numero di caratteri da confrontare. (Confronta byte per memcmp e caratteri wide per wmemcmp).

Valore restituito

Il valore restituito indica la relazione tra i buffer.

Valore restituito Relazione dei primi count caratteri di buf1 e buf2
< 0 buffer1 minore di buffer2
0 buffer1 uguale a buffer2
> 0 buffer1 maggiore di buffer2

Osservazioni:

Confronta i primi count caratteri di buffer1 e buffer2 e restituisce un valore che ne indica la relazione. Il segno di un valore restituito diverso da zero è il segno della differenza tra la prima coppia di valori diversi nei buffer. I valori vengono interpretati come unsigned char per memcmp e come wchar_t per wmemcmp.

Requisiti

Ciclo Intestazione obbligatoria
memcmp <memory.h> oppure <string.h>
wmemcmp <wchar.h>

Per altre informazioni sulla compatibilità, vedere Compatibility (Compatibilità).

Librerie

Tutte le versioni della libreria di runtime C.

Esempio

// 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.

Vedi anche

Manipolazione del buffer
_memccpy
memchr, wmemchr
memcpy, wmemcpy
memset, wmemset
strcmp, wcscmp, _mbscmp
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l