Library Function strcmp in String Class

Library Function strcmp of string.h Header File

On this page we will discuss about library function strcmp in string class which is used in C.The C header file string.h contains a set of functions that can be used for performing various manipulation operations on strings. The strcmp function is used to compare two strings lexicographically. It returns an integer value that indicates the relationship between the strings.

Library Function strcmp in String Class

Library Function strcmp in String Class

In C programming language the strcmp function is included in string.h header file.

When comparing strings lexicographically, each character in the strings is compared one by one, starting with the first character in each string. If the characters are the same, the next pair of characters is compared, and so on, until a difference is found or until the end of both strings is reached.

Declaration of strcmp function

 int strcmp (const char* str1, const char* str2);

Parameters of strcmp function

The strcmp function takes two parameters: str1 and str2, which are pointers to null-terminated strings.

ParameterDescription
str1It is a pointer to the first string that needs to be compared.
str2It is a pointer to the second string that needs to be compared.

Return value of strcmp function

The strcmp function returns integer values on the bases of comparisons which are given below:

Return ValueDescription
0If str1 and str2 are equal, the function returns 0.
>0If str1 is lexicographically greater than str2, the function returns a positive integer.
<0If str1 is lexicographically less than str2, the function returns a negative integer.

Implementation of Library Function string.h strcmp

Example 1:

The following code shows the usage of strcmp function in C.
Run
#include <stdio.h>
#include <string.h>

int main(void) {
    
    // Initializing the two strings
    char *str1 = "apple";
    char *str2 = "banana";
    
    // comparing strings str1 and str2
    int result = strcmp(str1, str2);
    if (result < 0) {
        printf("%s is lexicographically less than %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s is lexicographically greater than %s\n", str1, str2);
    } else {
        printf("%s is lexicographically equal to %s\n", str1, str2);
    }
    return 0;
}


Output:

apple is lexicographically less than banana

Example 2:

You can also use the strcmp function to compare substrings within a string. For example, the following code compares the first three characters of str1 to the first three characters of str2:
Run
#include  <stdio.h>
#include  <string.h>

int main(void) {
    
    // Initializing the two strings
    char *str1 = "apple";
    char *str2 = "banana";
    
    // comparing strings str1 and str2
   int result = strcmp(str1, str2);
   if (result == 0) {
       printf("The first three characters of %s and %s are the same.\n", str1, str2);
   } else {
       printf("The first three characters of %s and %s are different.\n", str1, str2);
   }

return 0;
}

Output:

 The first three characters of apple and banana are different.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription