Copyright © tutorialspoint.com

C - strcmp function

previous

Synopsis:

#include <stdio.h>

int strcmp(char *string1, char *string2); 

Description:

The strcmp function compares the contents of string1 and string2 and returns a value indicating their relationship.

Return Value

  • if Return value if < 0 then it indicates string1 is less than string2

  • if Return value if > 0 then it indicates string2 is less than string1

  • if Return value if = 0 then it indicates string1 is equal to string2

Example

#include <stdio.h>

int main() {
  char string1[20];
  char string2[20];

  strcpy(string1, "Hello");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));

  strcpy(string1, "Helloooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));

  strcpy(string1, "Hellooo");
  strcpy(string2, "Hellooo");
  printf("Return Value is : %d\n", strcmp( string1, string2));

  return 0;
}

It will proiduce following result:

Return Value is : -111
Return Value is : 111
Return Value is : 0

previous

Copyright © tutorialspoint.com