Examples of strcmp() in C

Example 1. strcmp() behavior for identical strings

This program illustrates the behavior of the strcmp() function for identical strings.

C




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
 
int main()
{
     
    char first_str[] = "g f g";
    char second_str[] = "g f g";
     
    // Using strcmp()
    int res = strcmp(first_str, second_str);
     
    if (res==0)
        printf("Strings are equal");
    else
        printf("Strings are unequal");
     
    printf("\nValue returned by strcmp() is: %d" , res);
    return 0;
}


Output

Strings are equal
Value returned by strcmp() is: 0

Example 2. strcmp() behavior for the lexicographically greater first string

The below example demonstrates the strcmp() function behavior for the lexicographically greater first string.

C




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
int main()
{
    // z has greater ASCII value than g
    char first_str[] = "zfz";
    char second_str[] = "gfg";
     
    int res = strcmp(first_str, second_str);
     
    if (res==0)
        printf("Strings are equal");
    else
        printf("Strings are unequal");
         
    printf("\nValue of result: %d" , res);
     
    return 0;
}


Output

Strings are unequal
Value of result: 19

Example 3. strcmp() behavior for the lexicographically smaller first string.

The below example demonstrates the strcmp() function behavior for the lexicographically smaller first string.

C




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
int main()
{
    // b has less ASCII value than g
    char first_str[] = "bfb";
    char second_str[] = "gfg";
     
    int res = strcmp(first_str, second_str);
     
    if (res==0)
        printf("Strings are equal");
    else
        printf("Strings are unequal");
         
    printf("\nValue returned by strcmp() is: %d" , res);
     
     
    return 0;
}


Output

Strings are unequal
Value returned by strcmp() is: -5

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

...

Contact Us