memcmp
int memcmp ( const void * buffer1, const void * buffer2, size_t num ); | string.h |
cplusplus.com |
Compare two buffers.
Compares the fisrt num bytes of two memory blocks pointed by buffer1
and buffer2.
Parameters.
Return Value.
Returns a value indicating the relationship between the buffers:
For comparative purposes, each byte is considered as unsigned char.
return value description <0 buffer1 is less than buffer2 0 buffer1 is the same as buffer2 >0 buffer1 is greater than buffer2
Portability.
Defined in ANSI-C.
Example.
/* memcmp example */
#include <stdio.h>
#include <string.h>
main ()
{
char str1[256];
char str2[256];
int n, len1, len2;
printf ("Enter a sentence: "); gets(str1);
printf ("Enter another sentence: "); gets(str2);
len1=strlen(str1);
len2=strlen(str2);
n=memcmp ( str1, str2, len1>len2?len1:len2 );
if (n>0) printf ("'%s' is greater than '%s'",str1,str2);
else if (n<0) printf ("'%s' is less than '%s'",str1,str2);
else printf ("'%s' is the same as '%s'\n",str1,str2);
return 0;
}
Output:
Enter a sentence: 12345
Enter another sentence: 54321
'12345' is less than '54321'
See also.
memchr,
memcpy,
memset,
strncmp