Memcmp In C

The function is defined as int memcmp(const void ptr1, const void ptr2, size_t num). Within this function, the system validates whether the size of the initial elements in the object pointed to by ptr1 is less than, greater than, or equal to the size of the initial num elements in the object pointed to by ptr2. This function is declared within the string.h header file. Therefore, it is essential to include the header file in your program in order to utilize this function in your code.

Declaring the memcmp Function

The format for defining a function is as shown below:

Example

int memcmp(const void *strng1, const void *strng2, size_t n);

Parameters inside the function

The pointer defined within the function will reference the location of the initial object that is to be compared with the second object.

This pointer references the memory block of the second object.

The parameter 'n' specifies the amount of data bytes that need to be compared.

Returning Value after Computation

The function has the capability to provide three potential value ranges following the comparison of two byte sizes.

The return value is a non-negative whole number, indicating that the memory block 1's size exceeds the size of memory block 2.

The return value indicates a negative integer, indicating that the size of memory block 1 is smaller than the size of memory block 2.

It results in 0: indicating that the size of memory block 1 is equivalent to the size of memory block 2.

Implementing memcmp function in C Program

In the following code snippet, we will demonstrate the application of the memcmp function to compare two arrays.

Example

#include <stdio.h>
#include <string.h>
#define SIZE 20
int main ()
{
// initializing a  variable result
//store the result of comparison
int result = 0;
//define both objects that is to be compared
char strng1[SIZE] = "memoryblock1";
char strng2[SIZE] = "sizeofthememoryblock2";
result = memcmp(strng1, strng2, 8);
if(result > 0)
{
printf("The size of strng1 is more than the size of strng2");
}
else if(result < 0)
{
printf("The size of strng1 is less than the size of strng2");
}
else
{
printf("The size of strng1 is equal to the size of strng2");
}
return 0;
}

Output:

Output

[Program Output]

Explanation:

The result of the memcmp function will be a negative value. In the provided scenario, "memoryblock1" is placed in the initial block, while "sizeofthememoryblock2" is stored in the second block. The initial differing characters are 'm' and 's'. Since the ASCII value of 's' is higher than 'm', a negative integer will be returned.

Important Facts Related to the memcmp Function in C Programming

  • It is necessary to include the h header file in your code to use the memcmp function.
  • The parameter in memcmp function includes pointers which points to the objects and an integer 'n'. The n specifies the characters counting from the beginning until the comparison is made.
  • If the initial characters in both the object are equal, then the function will move to the second character of the object. The step will be repeated until it finds any non-identical character in the block or compares to the nth element.
  • If all the elements in the memory block are identical when compared, then the memcmp functions 0.

Let's write the code to demonstrate the implementation of the scenario described above.

Example

#include <stdio.h>
#include <string.h>
int main ()
{
int result = 0;
int ary1[] = {5,6,7};
int ary2[] = {5,6,7};
result = memcmp(ary1, ary2, 6);
if(result > 0)
{
printf("The size of ary1 is more than the size of ary2");
}
else if(result < 0)
{
printf("The size of ary1 is less than the size of ary2");
}
else
{
printf("The size of ary1 is equal to the size of ary2");
}
return 0;
}

Output:

Output

[Program Output]

Explanation:

In the scenarios mentioned, the two arrays are identical. Therefore, the memcmp function will yield 0, and this result will be saved in the designated variable. If the initial characters differ between the arrays and the character value in the first array is higher than the corresponding character in the second array, a positive integer will be returned by the function.

Let's create a code snippet that compares the value of the first object with the value of the second object to ensure the first object has a higher value.

Example

#include <stdio.h>
#include <string.h>
int main ()
{
int result = 0;
//for the first array 9 is greater than 5
// once it has evaluated unmatching characters, it wont compare further.
int ary1[] = {5,9,7,11};
int ary2[] = {5,5,7,12};
result = memcmp(ary1, ary2,20);
if(result > 0)
{
printf("The size of ary1 is more than the size of ary2");
}
else if(result < 0)
{
printf("The size of ary1 is less than the size of ary2");
}
else
{
printf("The size of ary1 is equal to the size of ary2");
}
return 0;
}

Output:

Output

[Program Output]

Explanation:

The next item in the arrays differs, leading the memcmp function to determine the greater element. In this instance, the element 9 in the first array is larger than the element 5 in the second array. Consequently, a positive value will be the outcome.

If the first object's initial non-matching character is smaller than the corresponding character in the second object, a negative integer will be the result value.

Let's explore a scenario where the result produced by the memcmp function will be negative.

Example

#include <stdio.h>
#include <string.h>
int main ()
{
int result = 0;
//for the first array 4 is less than 5
// once it has evaluated unmatching characters, it wont compare further.
int ary1[] = {4,9,7,11};
int ary2[] = {5,5,7,12};
result = memcmp(ary1, ary2,20);
if(result > 0)
{
printf("The size of ary1 is more than the size of ary2");
}
else if(result < 0)
{
printf("The size of ary1 is less than the size of ary2");
}
else
{
printf("The size of ary1 is equal to the size of ary2");
}
return 0;
}

Output:

Output

[Program Output]

Explanation:

The initial element within the arrays differs. Therefore, when memcmp is executed, it will determine the lesser element. In this scenario, the element 4 in the first array is considered lower than the element 5 in the second array. Consequently, a negative value will be the outcome.

The developer must select the quantity of characters or bytes for the comparison operation, a crucial decision that can impact the outcome significantly. It is imperative for the developer to validate and assign a suitable value to n.

  1. It is essential for the developer to guarantee that the specified number of bytes remains smaller than the size of the two objects. Failing to adhere to this guideline could lead to an inaccurate result being returned.
  2. Example
    
    #include <stdio.h>
    #include <string.h>
    int main ()
    {
    int result = 0;
    //both the arrays are equal
    int ary1[] = {5,5,7,12};
    int ary2[] = {5,5,7,12};
    //here the value of n is greater than size of both arr1 and arr2
    result = memcmp(ary1, ary2,1300);
    if(result > 0)
    {
    printf("The size of ary1 is more than the size of ary2");
    }
    else if(result < 0)
    {
    printf("The size of ary1 is less than the size of ary2");
    }
    else
    {
    printf("The size of ary1 is equal to the size of ary2");
    }
    return 0;
    }
    

Output:

Output

[Program Output]

Explanation:

In the provided program, both arrays are identical. They are equivalent, and the variable n has a greater value compared to the size of both arrays. As a consequence, the program is producing an inaccurate output.

Note: You should not use the memcmp function to make the comparison between structures as it may return erroneous results and is not safe because there is always a chance of a garbage value in the padding bytes.

Implementing Your Own memcmp Function

The memcmp function comes pre-defined in the C library and can be readily utilized by including the string.h header file in your program. The functions provided by the library are highly efficient and optimal for usage. Therefore, there is no need to define or develop your own memcmp function.

If you have knowledge of specific conditions or situations that can simplify the comparison process between objects, you can customize your memcmp function to enhance the program's straightforwardness.

If you wish to enhance the code, you can incorporate additional conditions into the user-defined memcmp function or opt to utilize the library function for performing the comparison.

Example

int user_def_memcmp(const void *strng1, const void *strng2, int length)
{
unsigned char *x = strng1;
unsigned char *y = strng2;
int comparisoncheck = 0;
//To check thaLogic Practiceers are pointing to same blocks.
if (strng1 == strng2)
{
return comparisoncheck;
}
while (length > 0)
{
if (*x != *y)
{ //Comparison between un matching charachters
comparisoncheck = (*x >*y)?1:-1;
break;
}
length--;
x++;
y++;
}
return comparisoncheck;
}

Input Required

This code uses input(). Please provide values below: