How to use the strcmp() function in C

The following example demonstrates how to use the strcmp() function in C:

C




// C Program to Demonstrate the use of strcmp() function
#include <stdio.h>
#include <string.h>
 
int main()
{
    // declaring two same string
    char* first_str = "Geeks";
    char* second_str = "Geeks";
 
    // printing the strings
    printf("First String: %s\n", first_str);
    printf("Second String: %s\n", second_str);
 
    // printing the return value of the strcmp()
    printf("Return value of strcmp(): %d",
           strcmp(first_str, second_str));
 
    return 0;
}


Output

First String: Geeks
Second String: Geeks
Return value of strcmp(): 0

C strcmp()

In C language, the <string.h> header file contains the Standard String Library that contains some useful and commonly used string manipulation functions. In this article, we will see how to compare strings in C using the function strcmp().

Similar Reads

What is strcmp() in C?

C strcmp() is a built-in library function that is used for string comparison. This function takes two strings (array of characters) as arguments, compares these two strings lexicographically, and then returns 0,1, or -1 as the result. It is defined inside header file with its prototype as follows:...

Syntax of strcmp() in C

strcmp(first_str, second_str );...

Parameters of strcmp() in C

This function takes two strings (array of characters) as parameters:...

Return Value of strcmp() in C

The strcmp() function returns three different values after the comparison of the two strings which are as follows:...

How to use the strcmp() function in C

The following example demonstrates how to use the strcmp() function in C:...

How strcmp() in C works?

...

Examples of strcmp() in C

C strcmp() function works by comparing the two strings lexicographically. It means that it compares the ASCII value of each character till the non-matching value is found or the NULL character is found. The working of the C strcmp() function can be described as follows:...

Conclusion

Example 1. strcmp() behavior for identical strings...

FAQs on strcmp() in C

...