C memcmp() function
C memcmp() function - compare bytes in memory
The memcmp() function compares the first n bytes of str1 and str2.
Use memcmp() function when:
- Comparing segments of raw data to determine if they are identical, especially when working with binary or non-string data.
- Need a precise and direct comparison without relying on any assumptions about null-termination, which is especially useful in low-level programming.
- Comparing parts of arrays or structures in cases where equality matters only within a defined length of bytes.
Syntax:
int memcmp(const void *str1, const void *str2, size_t n)
Parameters:
Name | Description | Required /Optional |
---|---|---|
str1 | First buffer. | Required |
str2 | Second buffer. | Required |
n | Number of characters to compare. | Required |
Return value from memcmp()
- if Return value < 0 then it indicates str1 is less than str2.
- if Return value > 0 then it indicates str2 is less than str1.
- if Return value = 0 then it indicates str1 is equal to str2.
Examples: C memcmp() function
Example 1: Compares specified portions of two strings to determine their equality
This memcmp() example compares specified portions of two strings to determine their equality. The program first compares the first 10 characters of two strings, then the first 12 characters, and finally 19 characters from two different strings. For each comparison, it uses memcmp() to check if the compared parts are identical, lexicographically less than, or greater than each other, and then displays the result. This approach highlights how memcmp() can be used to perform selective, byte-by-byte comparisons in a memory-safe way.
Code:
#include <stdio.h>
#include <string.h>
int main( void )
{
char str1[] = "12345678901400345678";
char str2[] = "12345678901297650033";
int result;
printf("Original text:");
printf("\n%s",str1);
printf("\n%s",str2);
printf( "\n\nCompare first 10 characters of the said two strings");
result = memcmp( str1, str2, 10 );
if( result < 0 )
printf( "\nFirst is less than second.\n" );
else if( result == 0 )
printf( "\nFirst is equal to second.\n" );
else
printf( "\nFirst is greater than second.\n" );
printf( "\n\nCompare first 12 characters of the said two strings");
result = memcmp( str1, str2, 12 );
if( result < 0 )
printf( "\nFirst is less than second.\n");
else if( result == 0 )
printf( "\nFirst is equal to second.\n");
else
printf( "\nFirst is greater than second.\n");
char str3[] = "12345678901100345678";
char str4[] = "12345678901297650033";
printf("\n\nOriginal text:");
printf("\n%s",str3);
printf("\n%s",str4);
printf( "\n\nCompare first 19 characters of the said two strings");
result = memcmp( str3, str4, 19 );
if( result < 0 )
printf( "\nFirst is less than second.\n");
else if( result == 0 )
printf( "\nFirst is equal to second.\n");
else
printf( "\nFirst is greater than second.\n");
}
Output:
Original text: 12345678901400345678 12345678901297650033 Compare first 10 characters of the said two strings First is equal to second. Compare first 12 characters of the said two strings First is greater than second. Original text: 12345678901100345678 12345678901297650033 Compare first 19 characters of the said two strings First is less than second.
Example 2: Comparing portions of two arrays
This example shows comparing the first 5 elements of two arrays of integers.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Declare two integer arrays
int arr1[] = {1, 2, 3, 4, 5, 6};
int arr2[] = {1, 2, 3, 4, 0, 6};
int result;
// Compare the first 5 elements (5 * sizeof(int) bytes)
result = memcmp(arr1, arr2, 5 * sizeof(int));
// Check the result of the comparison
if (result == 0) {
printf("The first 5 elements of arr1 and arr2 are equal.\n");
} else if (result < 0) {
printf("The first 5 elements of arr1 are less than arr2.\n");
} else {
printf("The first 5 elements of arr1 are greater than arr2.\n");
}
return 0;
}
Output:
The first 5 elements of arr1 are greater than arr2.
Explanation:
- Two integer arrays, arr1 and arr2, are declared.
- memcmp() is used to compare the first 5 elements (interpreted as bytes by sizeof(int)).
- Based on the comparison result, a message indicates whether the first 5 elements are equal, less than, or greater.
Example 3: Comparing Strings with mixed cases
This example illustrates comparing case-sensitive strings using memcmp().
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define two strings
char str1[] = "HelloWorld";
char str2[] = "helloworld";
int result;
// Compare the entire strings (length of str1 bytes)
result = memcmp(str1, str2, strlen(str1));
// Check the result of the comparison
if (result == 0) {
printf("The strings str1 and str2 are identical.\n");
} else if (result < 0) {
printf("The string str1 is lexicographically less than str2.\n");
} else {
printf("The string str1 is lexicographically greater than str2.\n");
}
return 0;
}
Output:
The string str1 is lexicographically less than str2.
Explanation:
- Two strings, str1 and str2, are defined with the same letters but different cases.
- memcmp() compares str1 and str2 character-by-character up to the length of str1.
- Based on the result, the program prints whether the strings are identical or if one is lexicographically greater than the other.
C Programming Code Editor:
Previous C Programming: C memchr()
Next C Programming: C memcpy()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-memcmp.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics