Javatpoint Logo
Javatpoint Logo

memcmp() in C

Description of memcmp()

The memcmp() function allows the user to make a comparison between the bytes of the two mentioned characters. Depending upon the result after the comparison, it can return a positive or negative integer value. The function can also return 0.

The function is represented as int memcmp(const void *strng1, const void *strng2, size_t n). In this function, the computer will checks that the size of the initial elements of the object strng1 is either less, more or equal to the size of the initial n elements of the object strng2. The function is defined in the string.h header file. So, it is necessary to import the header file in your program to use the function in your code.

Declaring the memcmp() Function

The syntax for declaring the function is as follows:

Parameters inside the function

strng1: The pointer declared inside the function will point to the location of the first object that will be compared to the second object.

Strng2: This pointer points to the second object's memory block.

n: It determines the number of bytes of data that are required to be compared.

Returning Value after Computation

The function can return three possible ranges of values after comparing the size of two bytes.

Return Value is a Positive Integer: This means that the size of memory block 1 is greater than that of memory block 2.

Return Value is a Negative Integer: This means that the size of memory block 1 is less than that of memory block 2.

It returns 0: This means that the size of memory block 1 equals the size of memory block 2.

Implementing memcmp() function in C Program

Below is the code depicting the use of memcmp(). In the code below, we will compare the two arrays.

Output:

memcmp() in C

Explanation:

The value returned by the memcmp() will be negative. In the above example, we have stored "memoryblock1" in the first block, and in the second block, we have saved the "sizeofthememoryblock2". The first set of different characters is 'm' and 's'. The value of s is greater than the value of m; therefore, it will return a negative integer.

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 us code to see the implementation of the above scenario

Output:

memcmp() in C

Explanation:

In the above examples, both arrays are equal. So, the memcmp() will return 0, which will be stored in the variable.
The function will return a positive integer whenever the first characters are different in both the arrays and the value of the character of the first array are greater than the value of the second array.

Let us implement a code where the value of the first object is more than the second object.

Output:

memcmp() in C

Explanation:

The second element in the arrays is different. So, the memcmp() will evaluate which element is greater. In this case element in the first array that is 9 is greater than the element in the second array that is 5. Thus, it will return a positive value for the result.

5. Suppose the initial non-identical character of the first object is less than the corresponding character of the second object. In this case, the value of the result will be a negative integer.

Let us see an example where the value returned by the memcmp() will be negative.

Output:

memcmp() in C

Explanation:

The very first element in the arrays is different. So, the memcmp() will evaluate which element is less. In this case element in the first array that is 4 is less than the element in the second array that is 5. Thus, it will return a negative value for the result.

The programmer needs to choose the number of characters or bytes for which he wants to perform the comparison. It can affect the result, so the programmer must ensure that a valid value is assigned to n.

6. The programmer should ensure that the number of bytes he enters should always be less than the size of both objects. Otherwise, it may return the wrong result.

Output:

memcmp() in C

Explanation:

In the above program, both the arrays are exactly the same. That is equal, and the size of n is greater than the size of both arrays. Thus, the program is returning the wrong result.

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 is already defined in the C library and can be easily used after including the string.h header file in your program. The functions defined in the library are the most efficient and effective to use. So, defining or creating your memcmp() function is not required.

If there are certain conditions or scenarios that you are aware of, that can reduce the complexity of performing the comparison between the objects. In that case, you can define your memcmp() function to increase the program's simplicity.

Below is the function definition of a user-defined memcmp() function. Many scenarios are not taken into account in this function. So, if you want to add more conditions, you can change the code accordingly or use the library function to perform the comparison.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA