Open In App

memcmp() in C

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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";

    // Use memcmp() to compare s1 and s2 up to
  	// length of s1
    res = memcmp(s1, s2, strlen(s1));

    // Check the result of memcmp
    if (res > 0)
        printf("s1 is greater");
    else if (res < 0)
        printf("s2 is greater");
    else
        printf("both are equal");

    return 0;
}

Output
s2 is greater

Explanation: In this context, memcmp() is used to compare the first strlen(s1) bytes of the two strings s1 and s2. It returns a positive value if s1 is lexicographically greater, a negative value if s2 is greater, or zero if both strings are identical up to the specified length.

This article covers the syntax, uses and common example of memcmp() function in C:

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";
    int l1 = strlen(s1);
    int l2 = strlen(s2);
    int l3 = strlen(s3);

    int n = l1;
    if (n > l2)  n = l2;
    if (n > l3) n = l3;

    // Compare first n bytes of s1 and s2
    int res = memcmp(s1, s2, n);
    if (res == 0)
        printf("s1 and s2 are equal.\n");
    else
        printf("s1 and s2 are not equal.\n");

    // Compare first strlen(s3) bytes of s1 and s3
    res = memcmp(s1, s3, strlen(s3));
    if (res == 0)
        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

Next Article
Article Tags :

Similar Reads