close
The Wayback Machine - https://web.archive.org/web/20230125175704/https://www.geeksforgeeks.org/strcmp-in-c-cpp/
Skip to content
Related Articles
Open in App
Not now

Related Articles

strcmp() in C/C++

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 22 Nov, 2022
Improve Article
Save Article

strcmp() is a built-in library function and is declared in <string.h> header file. This function takes two strings as arguments and compare these two strings lexicographically. 

Syntax:

int strcmp(const char *leftStr, const char *rightStr );

In the above prototype, function strcmp takes two strings as parameters and returns an integer value based on the comparison of strings.

  • strcmp() compares the two strings lexicographically means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
  • If first character in both strings are equal, then this function will check the second character, if this is also equal then it will check the third and so on
  • This process will be continued until a character in either string is NULL or the characters are unequal.

What does strcmp() return?

This function can return three different integer values based on the comparison:

  1. Zero ( 0 ): A value equal to zero when both strings are found to be identical. That is, That is, All of the characters in both strings are same.
All characters of strings are same

C




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
 
int main()
{
     
    char leftStr[] = "g f g";
    char rightStr[] = "g f g";
     
    // Using strcmp()
    int res = strcmp(leftStr, rightStr);
     
    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

Greater than zero ( >0 ): A value greater than zero is returned when the first not matching character in leftStr have the greater ASCII value than the corresponding character in rightStr or we can also say

If character in leftStr is lexicographically
after the character of rightStr 

CPP




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
int main()
{
    // z has greater ASCII value than g
    char leftStr[] = "zfz";
    char rightStr[] = "gfg";
     
    int res = strcmp(leftStr, rightStr);
     
    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

Less than Zero ( <0 ): A value less than zero is returned when the first not matching character in leftStr have lesser ASCII value than the corresponding character in rightStr.

If character in leftStr is lexicographically
before the character of rightStr

CPP




// C program to illustrate
// strcmp() function
#include<stdio.h>
#include<string.h>
int main()
{
    // b has less ASCII value than g
    char leftStr[] = "bfb";
    char rightStr[] = "gfg";
     
    int res = strcmp(leftStr, rightStr);
     
    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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!