Format
#include <string.h> /* also in <memory.h> */ int memcmp(const void *buf1, const void *buf2, size_t count);
Language Level: ANSI, XPG4, Extension
memcmp compares the first count
bytes of buf1 and buf2.
Return Value
memcmp returns a value indicating the
relationship between the two buffers as follows:
| Value | Meaning |
| Less than 0 | buf1 less than buf2 |
| 0 | buf1 identical to buf2 |
| Greater than 0 | buf1 greater than buf2 |
Example
This example reports the relation between
the two arguments passed to main to determine which, if either,
is greater.
#include <stdio.h> #include <string.h>
int main(int argc, char ** argv)
{
int len;
int result;
if ( argc != 3 )
{
printf( "Usage: %s string1 string2\n", argv[0] );
}
else
{
/* Determine the length to be used for comparison */
if (strlen( argv[1] ) > strlen( argv[2] ))
len = strlen( argv[1] );
else
len = strlen( argv[2] );
result = memcmp( argv[1], argv[2], len );
printf( "When the first %i characters are compared,\n", len );
if ( result == 0 )
printf( "\"%s\" is identical to \"%s\"\n", argv[1], argv[2] );
else
if ( result < 0 )
printf( "\"%s\" is less than \"%s\"\n", argv[1], argv[2] );
else
printf( "\"%s\" is greater than \"%s\"\n", argv[1], argv[2] );
}
return 0;
/********************************************************************
If the program is passed the arguments "firststring secondstring",
the output should be:
When the first 11 characters are compared,
"firststring" is less than "secondstring"
********************************************************************/
}
![]()
![]()
memccpy -- Copy Bytes
memchr -- Search
Buffer
memcpy -- Copy
Bytes
memicmp --
Compare Bytes
memmove -- Copy
Bytes
memset -- Set
Bytes to Value
strcmp --
Compare Strings
<stdio.h>
<string.h>