Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

strcmp() Function in C

Last Updated on January 9, 2024 by Ankit Kochar

The strcmp() function in C programming is a fundamental tool for string comparison. It allows developers to compare two strings character by character, determining whether they are identical or if one precedes the other in lexicographical order. This function is crucial in various applications, from basic string operations to complex algorithms where string comparison plays a pivotal role. Understanding its functionality and proper implementation is essential for C programmers to ensure accurate and efficient string handling within their programs.

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
In conclusion, the strcmp() function is a powerful tool for string comparison in C programming. Its ability to compare strings based on their character sequences makes it invaluable in various applications. However, developers must exercise caution while using this function, ensuring proper null-terminated strings and considering the return values for different comparison scenarios. With a solid grasp of its functionality and usage, programmers can leverage strcmp() to enhance the efficiency and accuracy of their C programs.

Frequently Asked Questions(FAQs) Related to strcmp Function in C

Here are some Frequently Asked Questions related to “strcmp Function in C”.

1. What does strcmp() do in C?
strcmp() compares two strings character by character and returns an integer value indicating their relationship based on lexicographical order.

2. What is the syntax of strcmp()?
The syntax for strcmp() is: int strcmp(const char str1, const char str2);

3. What values does strcmp() return?

  • If the strings are equal, it returns 0.
  • If the first differing character in str1 has a lower ASCII value than the corresponding character in str2, it returns a negative integer.
  • If the first differing character in str1 has a higher ASCII value than the corresponding character in str2, it returns a positive integer.

4. How does strcmp() differ from == for string comparison?
strcmp() compares the actual content of strings character by character, whereas == compares the memory addresses of the strings. Thus, strcmp() is used for content-based comparison.

5. Is strcmp() case-sensitive?
Yes, strcmp() is case-sensitive as it compares strings based on their ASCII values, including the distinction between uppercase and lowercase letters.

6. Can strcmp() handle NULL pointers?
No, passing NULL pointers to strcmp() can result in undefined behavior. It’s important to ensure that valid null-terminated strings are passed as arguments.

7. How can one use strcmp() for sorting strings?
By leveraging strcmp() within sorting algorithms (like bubble sort or quicksort), programmers can compare strings and rearrange them based on their lexicographical order.

8. Are there other similar functions to strcmp() in C?
Yes, C provides additional string comparison functions like strncmp(), which allows specifying the maximum number of characters to compare.

9. Does strcmp() consider string length during comparison?
No, strcmp() compares strings until it encounters a null terminator (‘\0’) in either string, regardless of their lengths.

10. Is strcmp() limited to ASCII characters only?
strcmp() operates on ASCII values, so it works with ASCII characters. However, its behavior might differ with other character encodings like UTF-8 in certain cases. For accurate handling of non-ASCII characters, other functions or libraries might be necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *