C library function - memcmp()
Description
C library function **int memcmp(const void str1, const void str2, size_t n)) Compares the memory area str1 with the memory area, comparing up to str2 's first n bytes.
Declaration
Here is the declaration of the memcmp() function.
int memcmp(const void *str1, const void *str2, size_t n)
Parameters
- str1 -- a pointer to the memory block.
- str2 -- a pointer to the memory block.
- n -- the number of bytes to be compared.
Return Value
- If the return value is < less than 0, it means str1 is less than str2.
- If the return value is > 0, it means str1 is greater than str2.
- If the return value is = 0, it means str1 is equal to str2.
Example
The following example demonstrates the use of the memcmp() function.
Example
#include
#include
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's compile and run the above program, which will produce the following result:
str2 小于 str1
C Standard Library -