memcmp - C string.h

C examples for string.h:memcmp

From

<cstring> 
<string.h>

Description

Compare two blocks of memory

Type

function

Prototype

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

Parameters

Parameter Description
ptr1 Pointer to the first block of memory.
ptr2 Pointer to the second block of memory.
num Number of bytes to compare.

Return Value

return value indicates
<0 ptr1 has a lower value than ptr2
0 the contents of both memory blocks are equal
>0 ptr1 has a greater value than ptr2

Demo Code


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

int main ()//from ww w.j a v a2s.c o m
{
  char buffer1[] = "this";
  char buffer2[] = "thip";

  int n;

  n=memcmp ( buffer1, buffer2, sizeof(buffer1) );

  if (n>0) 
     printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) 
     printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else 
     printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}

Related Tutorials