In programming, many times we need to make a comparison between two values. The C programming language provides a special function known as the strcmp() Function. In this article, we will learn about the strcmp Function in C in detail along with its syntax and examples.
What is strcmp Function in C?
The strcmp Function in C is a built-in function that is used for comparing two strings. The strcmp Function in C is defined in the string.h header file. It is one of the commonly used functions in C programming. It compares two strings and returns an integer value that indicates whether the provided strings are equal or not.
Syntax of strcmp Function in C
The syntax of the strcmp Function in C is given as follows:
int strcmp(const char *str1, const char *str2 );
This function compares two strings lexicographically and returns the answer by comparing two strings str1 and str2.
Parameters of strcmp Function in C
strcmp Function in C takes two parameters that are listed below.
- *str1: It is a constant pointer pointing to the location of the first string (here, it is named “str1”).
- *str2: This is also a constant pointer pointing to the second string (here, str2).
Return Type of strcmp Function in C
The strcmp Function in C can return the following integer values.
- 0: If both of the strings that are passed are equal, the strcmp Function in C returns 0 as an answer.
- Greater than 0: The strcmp Function returns a value greater than 0 if the ASCII value of the first unmatched character of the first string(str1) is greater than that of the value of the corresponding character in the second string (str2). It returns the difference between the ASCII values of the first unmatched character of the strings.
- Lesser than 0: If the ASCII value of the first unmatched character of the first string(str1) is less than that of the corresponding character in the second string(str2), then it returns the difference between ASCII values of the first unmatched characters, which will be lesser than 0 in this case.
Examples of strcmp() Function in C
Here are some examples to learn more about the strcmp Function in C.
Example 1 of strcmp Function in C
This example compares two equal strings using the strcmp Function in C.
Code
#include<stdio.h> #include<string.h> int main() { char str1[] = "PrepBytes"; char str2[] = "PrepBytes"; int ans = strcmp(str1, str2); if (ans==0) printf("Strings are equal"); else printf("Strings are unequal"); printf("\nValue of result: %d" , ans); return 0; }
Output
Strings are equal
Value of result: 0
Explanation:
In this example, we have declared two character arrays named str1 and str2 and initialized them with the same value. Next, we compared the strings using the strcmp Function in C and stored the value in the variable named ans. Then on the basis of the value stored in the “ans” variable, we printed the Statements on the screen. Since we have initialized both of them with the same value, we get the expected result as “Strings as equal” on the output screen.
Example 2 of strcmp Function in C
This code takes a lexicographically greater string as the first argument.
Code
#include<stdio.h> #include<string.h> int main() { //ASCI value of A is less than P char str1[] = "PrepBytes"; char str2[] = "ArepBytes"; int ans = strcmp(str1, str2); if (ans==0) printf("Strings are equal"); else printf("Strings are unequal"); printf("\nValue of result: %d" , ans); return 0; }
Output
Strings are unequal
Value of result: 15
Explanation:
In the above code, we have declared two character arrays named str1 and str2. But this time we initialized str1 with “PrepBytes” and str2 with “ArepBytes”. Since the str1 is lexicographically greater than the str2. So strcmp Function in C will return a value greater than 0. The same behavior is shown by the output in the console screen after running the above code.
Example 3 of strcmp Function in C
Let us now pass a lexicographically smaller string in the first argument.
Code
#include<stdio.h> #include<string.h> int main() { //ASCI value of Z is more than P char str1[] = "PrepBytes"; char str2[] = "ZrepBytes"; int ans = strcmp(str1, str2); if (ans==0) printf("Strings are equal"); else printf("Strings are unequal"); printf("\nValue of result: %d" , ans); return 0; }
Output
Strings are unequal
Value of result: -10
Explanation:
In this code, we have initialized str1 with “PrepBytes” and str2 with “ZrepBytes”. Since str1 is lexicographically smaller, the strcmp Function in C must return a negative value as an answer. The output we get verifies the above statement.
Conclusion
The strcmp function in C is a very useful function, especially when working with strings. It can be used to compare two strings and check if they are equal or not. In this article, we have learned about its syntax and also discussed some of the examples of the strcmp Function in C. It is important to understand the syntax and usage of the strcmp function to use it effectively in C programming.
Frequently Asked Questions(FAQs)
Here are some Frequently Asked Questions related to “strcmp Function in C”.
Ques 1. Does the strcmp Function in C consider case sensitivity?
Ans. Yes, the strcmp Function in C is case-sensitive, meaning it considers uppercase and lowercase letters as different characters.
Ques 2. Can the strcmp Function in C be used to compare non-string variables?
Ans. No, the strcmp Function in C is specifically designed to compare strings, and cannot be used to compare other types of variables.
Ques 3. What happens if either or both of the strings passed to strcmp() are null?
Ans. If either or both of the strings passed to strcmp() are null, the function will likely crash or produce unpredictable results.
Ques 4. Can the strcmp Function in C be used to compare strings in a case-insensitive manner?
Ans. No, strcmp() is case-sensitive and cannot be used to compare strings in a case-insensitive manner. However, there are other functions such as strcasecmp() that can be used for this purpose.
Ques 5. Is strcmp() a standard function in C?
Ans. Yes, strcmp() is a standard function in the C programming language and is included in the string.h header file.