<string.h> Functions

C strcmp()

The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.

C strcmp() Prototype

The function prototype of strcmp() is:

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

strcmp() Parameters

The function takes two parameters:

  • str1 - a string
  • str2 - a string

Return Value from strcmp()

Return Value Remarks
0 if strings are equal
>0 if the first non-matching character in str1 is greater (in ASCII) than that of str2.
<0 if the first non-matching character in str1 is lower (in ASCII) than that of str2.

The strcmp() function is defined in the string.h header file.

Example: C strcmp() function

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

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

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

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

  return 0;


strcmp(str1, str2) = 1
strcmp(str1, str3) = 0

In the program,

  • strings str1 and str2 are not equal. Hence, the result is a non-zero integer.
  • strings str1 and str3 are equal. Hence, the result is 0.
Did you find this article helpful?