In C programming we need to compare two strings sometimes. In this case, what will you do? As a basic C programmer, you may write the following statement to compare two strings:


if(str1 == str2)
if(str1 == "Coder")

But unfortunately it’s invalid in C. You can’t compare two strings in such a way. Then what can you do? One thing you may do – compare character by character using a loop. This comparison will be run until a mismatch is found or any string of them terminates into a null character. Obviously I mean the following programs:

#include <stdio.h>
#include <string.h>
 
int main()
{
    int i;
    char str1[15] = "Bangladesh";
    char str2[15] = "Bangla";
    for(i=0; str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0'; i++){
 
    }
    if(str1[i] == '\0' && str2[i] == '\0'){
        printf("Match\n");
    }
    else{
        printf("Mismatch\n");
    }
    return 0;
}

Output:

Mismatch

#include <stdio.h>
#include <string.h>
 
int main()
{
    int i;
    char str1[15] = "Bangladesh";
    char str2[15] = "Bangladesh";
    for(i=0; str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0'; i++){
 
    }
    if(str1[i] == '\0' && str2[i] == '\0'){
        printf("Match\n");
    }
    else{
        printf("Mismatch\n");
    }
    return 0;
}

Match

Now let me explain the programs stated above. In 1st program, str1 & str2 aren’t same. So, the output is Mismatch indicating to the inequality of these two strings. Besides, in 2nd program, str1 & str2 are same. So, the output is Match indicating to the equality of these two strings.

You can compare two strings in such a way but think about this lengthy process. If you want to be efficient in C programming, then it’s a wrong process where there is a string library function, defined in header file, to compare two strings. Obviously that is strcmp() function.

strcmp() function compares two null-terminated byte strings. If these strings are equal, then strcmp() returns the value 0. Otherwise, it returns a numeric difference between the first mismatching characters based on ASCII value. If the value is negative, the 1st string is alphabetically above the 2nd string. For opposite action, the result is opposite too. The basic form of strcmp() function is given below:

**strcmp(str1, str2);**

Here str1 & str2 are two null-terminated byte strings and the parameters of strcmp() function. This function works by these parameters. Now take a look at this example:

#include <stdio.h>
#include <string.h>
 
int main()
{
    int i;
    char str1[15] = "Bangladesh";
    char str2[15] = "Bangladesh";
    char str3[15] = "Bangla";
    if(strcmp(str1, str2) == 0){
        printf("Match\n");
    }
    else{
        printf("Mismatch\n");
    }
    if(strcmp(str1, str3) == 0){
        printf("Match\n");
    }
    else{
        printf("Mismatch\n");
    }
    if(strcmp("Coder", "Coding") == 0){
        printf("Match\n");
    }
    else{
        printf("Mismatch\n");
    }
    return 0;
}

Output:

Match

Mismatch

Mismatch

I think now the use of this function is clear to you. I hope from now you will use this function to compare two strings. If you have any question, just shout in the comment box given below. Happy Coding!!!