Name

memcmp

Synopsis

Compares two memory blocks

#include <string.h>
intmemcmp(const void *b1, const void *b2, size_t n );

The memcmp() function compares the contents two memory blocks of n bytes, beginning at the addresses in b1 and b2, until it finds a byte that doesn’t match. The function returns a value greater than zero if the first mismatched byte (evaluated as unsigned char) is greater in b1, or less than zero if the first mismatched byte is greater in b2, or zero if the two buffers are identical over n bytes.

Example

long setone[5] = { 1, 3, 5, 7, 9 };
long settwo[5] = { 0, 2, 4, 6, 8 };

for ( int i = 0; i < 5; i++ )
  settwo[i] += 1;

if (memcmp( &setone, &settwo, sizeof(settwo) ) == 0 )
  printf( "The two arrays are identical, byte for byte.\n" );

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.