memcmp() in C
Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on the result after the comparison, it could go back to a high-quality or negative integer value.
This feature also can go back to 0. This function is represented as int memcmp(const void *str1, const void *str2, size_t n). In this characteristic, the laptop examines whether the dimensions of the primary element of item str1 are less than, extra than, or identical to the size of n initial factors of object str2. This characteristic is defined in the string.h header file. Consequently, you must import the header file into your program to use the functions in your code.
Declaration of the memcmp() function in C:
The syntax of the memcmp() function in C is given below –
int memcmp (const void *str1, const void *str2, size_t n);
Header file of the memcmp() function in C:
Header file is very important for execute any program. The header file of the memcmp() function in C is a string. h. So, it is written below:
include <string.h>
Parameters of the memcmp() function in C:
str1 – str1 is a pointer to a memory block. This pointer declared within the function points to the position of the first object compared to the second object.
str2 – str2 is a pointer to a block of memory.
n - n is the number of bytes to compare.
Return value of the memcmp() function in C:
- The return value is a positive integer: Memory block one is greater than memory block 2.
- The return value is a negative integer: Memory block one is less than memory block 2.
- The return value is 0: The size of memory block one must be equal to the size of memory block 2.
Example 1: The example of the memcmp() function in C is given below –
#include<stdio.h>
#include<string.h>
int main()
{
char str1[15];
char str1[15];
int ret;
memcpy(str1,”abcdefg”, 7);
memcpy(str2,”ABCDEFG”, 7);
ret = memcmp(str1, str2, 5);
if(ret > 0)
{
printf (“str2 is less than str1”);
}
else if(ret > 0)
{
printf (“str1 is less than str2”);
}
else {
printf (“str2 is equal to str1”);
}
return 0;
}
}
Result: We compile the above written program and also run this program. After compilation, the output is:
str2 is less than str1
The size of array1 is equal to the size of array2
Explanation of the above program: The value returned by memcmp() is negative. The second element of the array is different. So memcmp() evaluates which element is lesser. Therefore a negative integer is returned.
Example 2: The give another example of the memcmp() function in C is given below –
#include <stdio.h>
#include <string.h>
int main ()
{
int result = 0;
int ary1[ ] = {1,2,3};
int ary2[ ] = {1,2,3};
result = memcmp(ary1, ary2, 6);
if(result > 0)
{
prints("The size of array1 is greater than the size of array2");
}
else if(result < 0)
{
printf("The size of array1 is less than the size of array2");
}
else
{
printf("The size of array1 is equal to the size of array2");
}
return 0;
}
Result: We compile the above written program and also run this program. After compilation, the output is:
The size of array1 is equal to the size of array2
Explanation of the above program: Both arrays are the same in the example above. So memcmp() returns 0, which is stored in the variable.
Example 3: The give another example of the memcmp() function in C is given below –
#include <stdio.h>
#include <string.h>
int main ()
{
int result = 0;
int ary1[] = {5,5,7,12};
int ary2[] = {5,5,7,12};
result = memcmp(ary1, ary2,1300);
if(result > 0)
{
printf("The size of array1 is greater than the size of array2");
}
else if(result < 0)
{
printf("The size of array1 is less than the size of array2");
}
else
{
printf("The size of array1 is equal to the size of array2");
}
return 0;
}
Result: We compile the above written program and also run this program. After compilation, the output is:
The size of array1 is greater than the size of array2
Explanation of the above program: The second element of the array is different. So memcmp() evaluates which element is bigger and the resulting value is a negative integer.
So, here we discuss about the memcmp() function in C. We share some example and output value of it.