<string.h> Functions

Join our newsletter for the latest updates.

C strcmp()

In this tutorial, you will learn to compare two strings using the strcmp() function.

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
non-zero if strings are not equal

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.