RAD Studio
ContentsIndex
PreviousUpNext
memcmp

Header File 

mem.h, string.h  

Category 

Memory and String Manipulation Routines, Inline Routines 

Prototype 

int memcmp(const void *s1, const void *s2, size_t n); 

Description 

Compares two blocks for a length of exactly n bytes. 

memcmp is available on UNIX System V systems. 

memcmp compares the first n bytes of the blocks s1 and s2 as unsigned chars. 

Return Value 

Because it compares bytes as unsigned chars, memcmp returns a value that is

  • < 0 if s1 is less than s2
  • = 0 if s1 is the same as s2
  • > 0 if s1 is greater than s2
For example, 

memcmp(“\xFF”, “\x7F”, 1) 

returns a value greater than 0.

Note: If you are using the intrinsic version of these functions, the case of n = 0 will return NULL.
Example

#include <stdio.h>
#include <string.h>
int main(void)
{
   char *buf1 = "aaa";
   char *buf2 = "bbb";
   char *buf3 = "ccc";
   int stat;
   stat = memcmp(buf2, buf1, strlen(buf2));
   if (stat > 0)
      printf("buffer 2 is greater than buffer 1\n");
   else
      printf("buffer 2 is less than buffer 1\n");
   stat = memcmp(buf2, buf3, strlen(buf2));
   if (stat > 0)
      printf("buffer 2 is greater than buffer 3\n");
   else
      printf("buffer 2 is less than buffer 3\n");
   return 0;
}

Portability

POSIX 
Win32 
ANSI C 
ANSI C++ 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!