Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - memcmp()

C library function - memcmp()

C Standard Library - <string.h> C Standard Library -

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

Return Value

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 - <string.h> C Standard Library -