In C, memcmp() compares two memory blocks byte by byte. It looks at at most n bytes. It does not stop early at a '\0'.
You pass both blocks and the count. All three are required. The result is 0 when the compared bytes match, less than 0 when the first comes first, or greater than 0 when the first comes after. Include <string.h>.
Syntax
Pass both blocks and the count. Start with this call.
C
memcmp(a, b, 4);
Call
What it does
memcmp(a, b, 4)
Compares the first 4 bytes of a and b.
Include <string.h>. Only the sign of the result is portable.
Library declarations
C
int memcmp(const void *s1, const void *s2, size_t n);
Parameters
Parameter
Description
s1
Required. The first memory block.
s2
Required. The second memory block.
n
Required. How many bytes to compare.
Return Value
Return Value
Description
0
The compared bytes are equal.
Less than 0
The first block comes before the second.
Greater than 0
The first block comes after the second.
C Examples
Example 1 — Test Equality of n Bytes
Compare two strings of the same length. A result of 0 means every compared byte matches.