memcmp() in C

Last Updated : 24 Sep, 2026

In C, memcmp() is a built-in function used to compare the given number of bytes of data pointed by two pointers passed as arguments. It returns an integer indicating whether the first memory block is less than, equal to, or greater than the second.

Example:

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

int main() {
    int res = 0;
    char s1[10] = "geeks";
    char s2[10] = "greeks";

    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    size_t n = (len1 < len2) ? len1 : len2;

    // Compare the common bytes of both strings
    res = memcmp(s1, s2, n);

    if (res > 0)
        printf("s1 is greater");
    else if (res < 0)
        printf("s2 is greater");
    else if (len1 == len2)
        printf("both are equal");
    else if (len1 < len2)
        printf("s1 is smaller");
    else
        printf("s2 is smaller");

    return 0;
} 

Output
s2 is greater

Explanation: memcmp() compares the first n bytes of the two memory blocks. Here, n is the length of the shorter string. If all compared bytes are equal, the lengths of the two strings are checked to determine whether the complete strings are equal.

memcmp() compares raw bytes in memory. It does not specifically compare null-terminated C strings.

Syntax

memcmp() is a standard library function defined in <string.h> header file in C.

memcmp( p1, p2, n);

Parameters:

  • p1 and p2: Pointers to the memory block to compare.
  • n: Number of bytes to compare starting from the beginning.

Return Value:

This function returns:

  • 0: Both memory block are equal upto n bytes.
  • <0: First block is less then to second block upto n bytes.
  • >0: First block is greater than to second block upto n bytes.

Note: If n = 0, then it returns 0, even if the memory block is different.

Examples of memcmp()

The following examples demonstrate the use of memcmp() in C program.

Check String are Equal or Not

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

int main() {
    char s1[] = "GfG";
    char s2[] = "GfG";
    char s3[] = "gfg";

    size_t l1 = strlen(s1);
    size_t l2 = strlen(s2);
    size_t n = (l1 < l2) ? l1 : l2;

    // Compare the common bytes of s1 and s2
    int res = memcmp(s1, s2, n);

    if (res == 0 && l1 == l2)
        printf("s1 and s2 are equal.\n");
    else
        printf("s1 and s2 are not equal.\n");

    size_t l3 = strlen(s3);
    n = (l1 < l3) ? l1 : l3;

    // Compare the common bytes of s1 and s3
    res = memcmp(s1, s3, n);

    if (res == 0 && l1 == l3)
        printf("s1 and s3 are equal.");
    else
        printf("s1 and s3 are not equal.");

    return 0;
} 

Output
s1 and s2 are equal.
s1 and s3 are not equal.

Check Arrays are Equal or Not

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

int main()
{
    int s1[] = {10, 20, 30, 40};
    int s2[] = {10, 20, 35, 40};

    // Compare first sizeof(s1) bytes of s1 and s2
    int res = memcmp(s1, s2, sizeof(s1));

    if (res == 0)
        printf("Arrays are identical");
    else
        printf("Arrays differ");

    return 0;
}

Output
Arrays differ

Behavior of memcmp() when Third Parameter is 0

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

int main()
{
    int v1[] = {'a', 'b', 'c'};
    int v2[sizeof(v1)] = {'d', 'e', 'f'};

    int res = memcmp(v1, v2, 0);

    if (res == 0)
        printf("Arrays are identical");
    else
        printf("Arrays differ");

    return 0;
}

Output
Arrays are identical
Comment