strcmp() C Library Function Usage with Examples

C programming standard library provides strcmp() function in order to compare two strings and return the results whether they are identical or different.

Syntax and Parameters

As stated previously strcmp() function takes two char array or string arguments.

int strcmp (const char* str1, const char* str2);
  • const char* str1 is the first string or char array which will be compared the second one. const is mainly used to prevent given char array pointer to be changed.
  • const char* str2 is the second string or char array which will be compared with the first one.

Return Values

strcmp() function returns an int or integer type. We can get 3 types of return value that are explained below.

  • `0` is returned if both strings are identical, equal or the same.
  • `Negative Integer` if the ASCII value of the first unmatched character is less then second
  • `Positive Integer` if the ASCII value of the first unmatched character is greater than second

Compare Two Strings

We can compare two strings which are expressed as char array in C programming language. We will compare strings “I love poftut.com” and “I loves poftut.com” .

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "I love poftut.com", str2[] = "I loves poftut.com";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}

We will compile with gcc like below and then run the binary.

$ gcc strcmp.c -o strcmp
$ ./strcmp
Compare Two Strings
Compare Two Strings

Compare Two Char Array

We can compare two char arrays where

#include <stdio.h>
#include <string.h>

int main()
{
  char str1[] = "I love poftut.com", str2[] = "I love poftut.com";
  int result;

  // comparing strings str1 and str2
  result = strcmp(str1, str2);
  printf("strcmp(str1, str2) = %d\n", result);

return 0;
}

 

Compare Two Char Array
Compare Two Char Array

We can see that both of the character arrays are the same so the return value will be 0 and printed to the screen.

 

Compare Two Char Array
Compare Two Char Array

Strings Are Different and First Is Bigger and Return Positive Value

In this example, the first string is bigger and return a positive integer which is the value of the character.

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", str2[] = "aBcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}
Strings Are Different and First Is Bigger and Return Positive Value
Strings Are Different and First Is Bigger and Return Positive Value

Strings Are Different and Second Is Bigger Return Negative Value

In this example, the second string is bigger and return a positive integer which is the value of the character.

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "aBcd", str2[] = "abcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);
}
Strings Are Different and Second Is Bigger Return Negative Value
Strings Are Different and Second Is Bigger Return Negative Value

Strings Are Same Return 0

If both strings are the same the strcmp() function will return zero 0 .

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "abcd", str2[] = "abcd";
    int result;

    // comparing strings str1 and str2
    result = strcmp(str1, str2);
    printf("strcmp(str1, str2) = %d\n", result);

    return 0;
}
Strings Are Same Return 0
Strings Are Same Return 0

Leave a Comment