Strcmp() Function in C
The C language's built-in library contains the strcmp() function. Its definition and prototyping are contained in the "<string.h>" header file. The function compares and assesses the equality of strings by traversing them as parameters.
It compares strings lexicographically, which means that it looks for every character in both strings at every index.
The first character of strings is compared to each subsequent character until a mismatched or NULL character is found.
Syntax of strcmp() in C:
int strcmp(const char *user_string1, const char *user_string2)
The two strings that need to be compared in this case are user_string1 and user_string2. The function returns the values based on the certain conditions.
Basically, it has (3) three main conditions.
- Return value is greater than zero (>0).
- Return value is less than zero (<0).
- Return value is equal to zero (==0).
Return value is greater than zero (>0):
If the first unmatched character in the left-hand string (user_string1) has a higher ASCII value than the matching character in the right-hand string (user_string2), the return value will be larger than zero.
The difference between the ASCII values of the strings' initial unmatched characters, (user_string1-user_string2), is the resultant value.
The below is an example in which we can easily understand the returning a value greater than zero.
//C code demonstrating the strcmp() function for returning a value greater than zero #include<stdio.h> //adding the header file, which has the strcmp() function. #include<string.h> int main() { //string declaration and initialization char user_string1[100] ; printf("Enter the first string:"); scanf("%s",&user_string1); char user_string2[100] ; printf("Enter the second string:"); scanf("%s",&user_string2); // Invoking strcmp() and saving the outcome int result_value = strcmp(user_string1, user_string2); // checking for equality condition if (result_value == 0) printf("Strings are said to be equal"); else printf("Strings are said to be unequal"); // Printing the result of the strcmp() function. printf("\nThe value that strcmp() returned is: %d", result_value); return 0; }
OUTPUT:

Explanation:
- Two strings, user_string1 and user_string2, are defined and initialised, respectively, in the code above. The strcmp() function is then called on them. The variable result_value is used to store the function's return value.
- At index 0, where the characters in both strings are "s" and "i," respectively, the first unmatched character in the strings can be identified. Both characters have ASCII values of 115 and 105, respectively.
- Consequently, the ASCII value difference comes out to be 10. By examining if result_value equals zero or not, a conditional statement is utilised to determine whether the strings are identical or not.
Return value is less than zero (<0):
If the first mismatched character in the left-hand string (user_string1) has a lower ASCII value than the matching character in the right-hand string (user_string2), the return result will be less than zero.
The difference between the ASCII values of the strings' initial unmatched characters, (user_string1-user_string2), is the resultant value.
The below is an example in which we can easily understand the returning a value less than zero.
//C code demonstrating the strcmp() function less than zero #include<stdio.h> //adding the header file, which has the strcmp() function. #include<string.h> int main() { //string declaration and initialization char user_string1[100] ; printf("Enter the first string:"); scanf("%s",&user_string1); char user_string2[100] ; printf("Enter the second string:"); scanf("%s",&user_string2); // Invoking strcmp() and saving the outcome int result_value = strcmp(user_string1, user_string2); // checking for equality condition if (result_value == 0) printf("Strings are said to be equal"); else printf("Strings are said to be unequal"); // Printing the result of the strcmp() function. printf("\nThe value that strcmp() returned is: %d", result_value); return 0; }
OUTPUT:

Explanation:
- The first mismatched character in the strings is discovered in the code above at index 0, where S and Z, respectively, are characters from both strings.
- Both characters have ASCII values of 83 and 90, respectively. As a result, the ASCII value difference comes out to be -7.
Return value is equal to zero (==0):
In the event if both strings are identical, the return value is zero. In other words, elements in two strings with the same index are identical.
The below is an example in which we can easily understand the returning a value equal to zero.
//C code demonstrating the strcmp() function equal to zero #include<stdio.h> //adding the header file, which has the strcmp() function. #include<string.h> int main() { //string declaration and initialization char user_string1[100] ; printf("Enter the first string:"); scanf("%s",&user_string1); char user_string2[100] ; printf("Enter the second string:"); scanf("%s",&user_string2); // Invoking strcmp() and saving the outcome int result_value = strcmp(user_string1, user_string2); // checking for equality condition if (result_value == 0) printf("Strings are said to be equal"); else printf("Strings are said to be unequal"); // Printing the result of the strcmp() function. printf("\nThe value that strcmp() returned is: %d", result_value); return 0; }
OUTPUT:

Explanation:
- Two strings, user_string1 and user_string2, are defined and initialised, respectively, in the code above. They are subsequently given through matchStr(), a user-defined method.
- The strcmp() function is called in the matchStr() function, and the function's return value is saved in the variable rvalue.
- By examining if result_value equals zero or not, a conditional statement is utilised to determine whether the strings are identical or not.
Conclusion:
- A prototype and definition for the C built-in library function strcmp() may be found in the string.h header file.
- The two character arrays that need to be compared are sent as inputs to the strcmp() function.
The first mismatched character between the two strings is taken into account when the strcmp() function in the C language produces an integer result.