C strcmp() function with example

The strcmp() function in C is used for comparing two string and checking if they are equal or not. strcmp() function compares two strings character by character from the first character until the end of one of the string occurs then returns the result.

Basic Syntax

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

And the parameters are:

ParameterDescription
*leftStrThe pointer to the first character array.
*rightStrThis pointer to the second character array.

Return Value

This function returns the following values:

  • 0 : If both strings are equal means all characters in the string are same then it will return 0.
  • Greater than zero (Positive Integer) : It returns a value greater than zero when the ASCII value of the first unmatched character in leftStr is greater than second.
  • Less than zero (Negative) : It returns value less than zero when ASCII value of the first unmatched character in leftStr is less than second.

Example

Following is the example for strcmp() function in C.

The output should be:

LEAVE A REPLY

Please enter your comment!
Please enter your name here