The function int memcmp (const void * str1, const void * str2, size_t n)) compares the first n bytes of two strings str1 and str2.
Declare the function memcmp () in C
Below is the declaration for memcmp () in C:
int memcmp ( const void * str1 , const void * str2 , size_t n )
Parameters
str1 - This is the pointer to a memory block.
str2 - This is a pointer to a memory block.
n - This is the number of bytes to be compared.
Returns the value
If
If the value> 0 is returned, this function indicates that str2 is shorter than str1.
If the value = 0 is returned, this function indicates that str1 is equal to str2.
For example
The following C program illustrates the usage of memcmp () in C:
#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 ( "Chuoi str2 la ngan hon chuoi str1" ); } else if ( ret Compiling and running the above C program will result:
According to Tutorialspoint
Previous article: Function memchr () in C
Next lesson: Function memcpy () in C
ncG1vNJzZmismaXArq3KnmWcp51kwamxjJ%2Bsp5uknryvecyepJyloGK2r3nC