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;
}
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;
}
Outputs1 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;
}
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;
}
OutputArrays are identical
Similar Reads
wmemcmp() function in C/C++ The wmemcmp() function in C/C++ compare two wide characters. This function compares the first num wide characters of two wide characters pointed by str1 and str2, returns zero if both the strings are equal or different value from zero if they are not. Syntax: int wmemcmp (const wchar_t* str1, const
2 min read
scanf in C In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
C strcmp() In C, strcmp() is a built-in library function used to compare two strings lexicographically. It takes two strings (array of characters) as arguments, compares these two strings lexicographically, and then returns some value as a result.Let's take a look at an example:C#include <stdio.h> #inclu
5 min read
wmemchr() function in C/C++ The wmemchr() function in C/C++ Locate character in block of wide characters. This function searches within the first num wide characters of the block pointed by ptr for the first occurrence of ch, and returns a pointer to it. Syntax: const wchar_t* wmemchr( const wchar_t* ptr, wchar_t ch, size_t nu
3 min read
strrchr() in C The strrchr() function in C locates the last occurrence of a character in a string and returns a pointer to it. It is a standard library function defined inside <string.h> header file. Syntax : char* strrchr( char* str, int chr ); Parameter: str: specifies the pointer to the null-terminated st
2 min read