The function memcmp () in C
The function int memcmp (const void * str1, const void * str2, size_t n)) compares the first n bytes of two strings str1 and str2.
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 < 0 ) { printf ( "Chuoi str1 la ngan hon chuoi str2" ); } else { printf ( "Chuoi str1 la bang chuoi str2" ); } return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous article: Function memchr () in C
Next lesson: Function memcpy () in C
You should read it
- The function atoi () in C
- The memchr () function in C
- The getchar () function in C
- Call the function by value in C ++
- The atol () function in C
- The function strtod () in C
- Putchar () function in C
- Call the function by pointer in C ++
- The abort () function in C
- Function in programming C
- The rewind () function in C
- The ord () function in Python
Maybe you are interested
NASA announced its plan to search for extraterrestrial life Discovered mysterious signals emanating from a star right next to the Solar System, possibly of aliens NASA reveals its latest snapshot of the Martian surface with a resolution of 1.8 billion pixels Found the second planet of Proxima Centauri, the star closest to the sun The most remote and least-populated places on Earth 7 most modern astronaut ships on the planet