The function strcmp () in C
The function int strcmp (const char * str1, const char * str2) compares the string pointed to by sr1 with the string pointed to by srt2.
The function int strcmp (const char * str1, const char * str2) compares the string pointed to by sr1 with the string pointed to by srt2.
Declaring the function strcmp () in C
Here is the declaration for strcmp () in C:
int strcmp ( const char * str1 , const char * str2 )
Parameters
str1 - This is the first string to be compared.
str2 - This is the second string to be compared.
Returns the value
This function returns the following values:
If the value returns <0, this function indicates that str1 is shorter than str2.
If the value returns> 0, this function indicates that str2 is shorter than str1.
If the return value = 0, this function indicates that str1 is equal to str2.
For example
The following C program illustrates the usage of strncmp () in C:
#include #include int main () { char str1 [ 15 ]; char str2 [ 15 ]; int ret ; strcpy ( str1 , "abcdef" ); strcpy ( str2 , "ABCDEF" ); ret = strcmp ( str1 , str2 ); if ( ret < 0 ) { printf ( "Chuoi str1 la ngan hon chuoi str2" ); } else if ( ret > 0 ) { printf ( "Chuoi str2 la ngan hon chuoi str1" ); } else { printf ( "Chuoi str1 la bang chuoi str2" ); } return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous lesson: strchr () function in C
Next lesson: Function strncmp () in C
You should read it
- Memset () function in C
- The strrchr () function in C
- Strcpy () function in C
- Memmove function in C
- Function strxfrm () in C
- The strcoll () function in C
- The function strncmp () in C
- The strstr () function in C
- The function strpbrk () in C
- The strtok () function in C
- Strcat function in C
- The strlen () function in C
Maybe you are interested
Revealing new findings about the size of the galaxy Admire incredibly detailed images of the Orion Nebula through the eyes of the James Webb telescope This star 'tears up' its companion to create a rare beautiful nebula Galaxy merger moment gives glimpse into Milky Way's future Science has discovered the fastest-flying star in the Milky Way, reaching 8% of the speed of light Discovered organic molecules that can make life in the center of the Milky Way