strcmp() in C/C++

strcmp() is a built-in library function and is declared in <string.h> header file. This function takes two strings as arguments and compare these two strings lexicographically.

Syntax::

int strcmp(const char *leftStr, const char *rightStr );

In the above prototype, function srtcmp takes two strings as parameters and returns an integer value based on the comparison of strings.

  • strcmp() compares the two strings lexicographically means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
  • If first character in both strings are equal, then this function will check the second character, if this is also equal then it will check the third and so on
  • This process will be continued until a character in either string is NULL or the characters are unequal.

What does strcmp() return?

This function can return three different integer values based on the comparison:

  1. Zero ( 0 ): A value equal to zero when both strings are found to be identical. That is, That is, All of the characters in both strings are same.
    All characters of strings are same
    // C program to illustrate
    // strcmp() function
    #include<stdio.h>
    #include<string.h>
    
    int main()
    { 
    	
    	char leftStr[] = "g f g";
    	char rightStr[] = "g f g";
    	
    	// Using strcmp()
    	int res = strcmp(leftStr, rightStr);
    	
    	if (res==0)
    	    printf("Strings are equal");
    	else 
    	    printf("Strings are unequal");
    	
    	printf("\nValue returned by strcmp() is:  %d" , res);
    	return 0;
    }
    

    Output:

    Strings are equal
    Value returned by strcmp() is:  0
    
  2. Greater than zero ( >0 ): A value greater than zero is returned when the first not matching character in leftStr have the greater ASCII value than the corresponding character in rightStr or we can also say
    If character in leftStr is lexicographically after the character of rightStr 
    // C program to illustrate
    // strcmp() function
    #include<stdio.h>
    #include<string.h>
    int main()
    { 
    	// z has greater ASCII value than g
    	char leftStr[] = "zfz";
    	char rightStr[] = "gfg";
    	
    	int res = strcmp(leftStr, rightStr);
    	
    	if (res==0)
    	    printf("Strings are equal");
    	else 
    	    printf("Strings are unequal");
    	    
    	printf("\nValue of result: %d" , res);
    	
    	return 0;
    }
    

    Output:

    Strings are unequal
    Value returned by strcmp() is:  19
    
  3. Less than Zero ( <0 ): A value less than zero is returned when the first not matching character in leftStr have lesser ASCII value than the corresponding character in rightStr.
    If character in leftStr is lexicographically before the character of rightStr
    // C program to illustrate
    // strcmp() function
    #include<stdio.h>
    #include<string.h>
    int main()
    { 
    	// b has less ASCII value than g
    	char leftStr[] = "bfb";
    	char rightStr[] = "gfg";
    	
    	int res = strcmp(leftStr, rightStr);
    	
    	if (res==0)
    	    printf("Strings are equal");
    	else 
    	    printf("Strings are unequal");
    	    
    	printf("\nValue returned by strcmp() is:  %d" , res);
    	
    	
    	return 0;
    }
    

    Output:

    Strings are unequal
    Value returned by strcmp() is:  -5
    
  4. Important point : When the strings are not same, you will find that the value returned by the strcmp() function is the difference between the ASCII values of first unmatched character in leftStr and rightStr in both the cases.
     
    This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.