memcmp

From cppreference.com
< c‎ | string‎ | byte
Defined in header <string.h>
int memcmp( const void* lhs, const void* rhs, size_t count );

Compares the first count characters of the objects pointed to by lhs and rhs. The comparison is done lexicographically.

Contents

[edit] Parameters

lhs, rhs - pointers to the memory buffers to compare
count - number of bytes to examine

[edit] Return value

Negative value if lhs is less than rhs.

0 if lhs is equal to rhs.

Positive value if lhs is greater than rhs.

[edit] Example

#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char a1[] = {'a', 'b', 'c'};
    char a2[] = {'a', 'b', 'd'};
 
    printf("'abc' vs 'abd': %2d\n", memcmp(a1, a2, sizeof a1));
    printf("'abd' vs 'abc': %2d\n", memcmp(a2, a1, sizeof a1));
    printf("'abc' vs 'abc': %2d\n", memcmp(a1, a1, sizeof a1));
 
    return 0;
}

Output:

'abc' vs 'abd': -1
'abd' vs 'abc':  1
'abc' vs 'abc':  0

[edit] See also

compares two strings
(function)
compares a certain amount of characters of two strings
(function)
C++ documentation for memcmp