C library functions -memcmp()
C Standard Library - <string.h>
Description
C Library Functionsint memcmp(const void *str1, const void *str2, size_t n))Compare the storage areastr1and the storage areastr2the frontnCompare bytes.
Declaration
Below is the declaration of the memcmp() function.
int memcmp(const void *str1, const void *str2, size_t n)
Parameter
- str1-- Pointer to a block of memory.
- str2-- Pointer to a block of memory.
- n-- Number of bytes to be compared.
Return Value
- If the return value is < 0, it indicates that str1 is less than str2.
- If the return value is > 0, it indicates that str1 is greater than str2.
- If the return value is = 0, it indicates that str1 is equal to str2.
Example
The following example demonstrates the usage of the memcmp() function.
Example
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
memcpy(str1, "abcdef", 6);
memcpy(str2, "ABCDEF", 6);
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("str1 is equal to str2");
}
return(0);
}
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
memcpy(str1, "abcdef", 6);
memcpy(str2, "ABCDEF", 6);
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("str1 is equal to str2");
}
return(0);
}
Let us compile and run the above program, which will produce the following result:
str2 小于 str1other extensions