C memcmp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

Definition

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);
CallWhat 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

ParameterDescription
s1Required. The first memory block.
s2Required. The second memory block.
nRequired. How many bytes to compare.

Return Value

Return ValueDescription
0The compared bytes are equal.
Less than 0The first block comes before the second.
Greater than 0The 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.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *a = "Code";
    const char *b = "Code";
    size_t n = 4;

    if (memcmp(a, b, n) == 0) {
        printf("First %zu bytes match\n", n);
    } else {
        printf("Bytes differ\n");
    }

    return 0;
}

How It Works

  1. Pick a length. Both strings have four letters, so n = 4.
  2. Compare byte by byte. 'C', 'o', 'd', 'e' match in both blocks.
  3. Check for zero. memcmp == 0 means equality for those n bytes.
  4. For full C-strings. Prefer strcmp(a, b) == 0 when both are null-terminated and you want whole-string equality.

Example 2 — Less Than / Greater Than

When bytes differ, the first mismatch decides which block is smaller or larger.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *s1 = "ABC";
    const char *s2 = "ABD";
    int r = memcmp(s1, s2, 3);

    if (r < 0) {
        printf("s1 is less than s2\n");
    } else if (r > 0) {
        printf("s1 is greater than s2\n");
    } else {
        printf("equal\n");
    }

    return 0;
}

How It Works

  1. Match the prefix. Indices 0 and 1 are 'A' and 'B' in both strings.
  2. Find the mismatch. Index 2 is 'C' vs 'D'.
  3. Compare as unsigned char. 67 < 68, so the result is negative.
  4. Use the sign only. Do not depend on the exact integer returned by your library.

Example 3 — Compare Binary Buffers

memcmp() works when there is no null terminator—ideal for packets and raw bytes.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    unsigned char a[] = { 0x01, 0x02, 0xFF };
    unsigned char b[] = { 0x01, 0x02, 0xFE };
    int r = memcmp(a, b, sizeof a);

    if (r == 0) {
        printf("buffers equal\n");
    } else if (r > 0) {
        printf("a is greater (0xFF > 0xFE)\n");
    } else {
        printf("a is less\n");
    }

    return 0;
}

How It Works

  1. Use sizeof for length. Both arrays have three bytes, so n = 3.
  2. Match the prefix. 0x01 and 0x02 are the same in both buffers.
  3. Compare the marker byte. 0xFF (255) is greater than 0xFE (254).
  4. Prefer memcmp over strcmp here. There is no '\0' end marker; strcmp would be the wrong tool.

Frequently Asked Questions

memcmp() compares two memory blocks for up to n bytes and returns 0, less than 0, or greater than 0.
All three are required. s1 and s2 are the blocks, and n is how many bytes to compare.
0 when the bytes match. A negative value means the first block comes first. A positive value means the first comes after.
Include <string.h>.
No. It compares exactly n bytes.

Next: memcpy()

Copy a fixed number of bytes from one memory block to another.

memcpy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful