Name

memcmp - compares memory areas

Library

libc.lib

Synopsis

  #include <string.h>
  int memcmp (const void *b1, const void *b2, size_t len);

Return values

The memcmp function returns zero if the two strings are identical, otherwise returns the difference between the first two differing bytes (treated as unsigned char values, so that '\200' is greater than '\0,' for example). Zero-length strings are always identical.

Detailed description

The memcmp function compares byte string b1 against byte string b2. Both strings are assumed to be len bytes long.

Examples

#include <string.h>
#include <stdio.h>
int main()
{
   char str1[]  = "abcdefg";
   char str2[] =  "abcdefr";
   int result;
   printf( "Compare ’%.6s’ to ’%.6s\n", str1, str2 );
   result = memcmp( str1, str2, 6);
   if( result < 0 )
      printf( "str1 is less than str2.\n" );
   else if( result == 0 )
      printf( "str1 is equal to str2.\n" );
   else if( result > 0 )
      printf( "str1 is greater than str2.\n" );
   printf( "Compare ’%.7s’ to ’%.7s\n", str1, str2 );
   result = memcmp( str1, str2, 7 );
   if( result < 0 )
      printf( "str1 is less than str2.\n" );
   else if( result == 0 )
      printf( "str1 is equal to str2.\n" );
   else if( result > 0 )
      printf( "str1 is greater than str2.\n" );
   return 0;    
}

         

Output

Compare ’abcdef’ to ’abcdef
str1 is equal to str2.
Compare ’abcdefg’ to ’abcdefr
str1 is less than str2.

         

See also

bcmp, strcasecmp, strcmp, strcoll, strxfrm

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top