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
4.7 ★ | 3 Vote
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
Difference between function and formula in Excel
8 little-known Excel functions that can save you a lot of work
How to use the NORMDIST function in Excel - Function that returns the distribution in Excel
Date functions in Excel, DAY, WEEKDAY, MONTH
How to use the SUMIF function in Excel to calculate the sum based on conditions
How to use the Round function in Excel to round numbers and process data