strcmp -- Compare Strings

Format

#include <string.h>
int strcmp(const char *string1, const char *string2);

Language Level: ANSI, POSIX, XPG4
strcmp compares string1 and string2. The function operates on null-terminated strings. The string arguments to the function should contain a null character (\0) marking the end of the string.

Return Value
strcmp returns a value indicating the relationship between the two strings, as follows:

Value Meaning
Less than 0 string1 less than string2
0 string1 identical to string2
Greater than 0 string1 greater than string2.

If count is greater than the length of string1 or string2, characters that follow a null character are not compared.

Example
This example compares the two strings passed to main using strcmp.

#include <stdio.h>
#include <string.h>
int main(int argc, char ** argv)
{
  int  result;
  if ( argc != 3 )
  {
    printf( "Usage: %s string1 string2\n", argv[0] );
  }
  else
  {
    result = strcmp( argv[1], argv[2] );
    if ( result == 0 )
      printf( "\"%s\" is identical to \"%s\"\n", argv[1], argv[2] );
    else if ( result < 0 )
      printf( "\"%s\" is less than \"%s\"\n", argv[1], argv[2] );
    else
      printf( "\"%s\" is greater than \"%s\"\n", argv[1], argv[2] );
  }
  return 0;
  /*****************************************************************
     If the following arguments are passed to this program:
     "is this first?" "is this before that one?"
     The output should be:
     "is this first?" is greater than "is this before that one?"
  *****************************************************************/
}


strcat -- Concatenate Strings
strchr -- Search for Character
strcmpi -- Compare Strings Without Case Sensitivity
strcpy -- Copy Strings
strcspn -- Compare Strings for Substrings
stricmp -- Compare Strings as Lowercase
strncmp -- Compare Strings
strnicmp -- Compare Strings Without Case Sensitivity
strpbrk -- Find Characters in String
strrchr -- Find Last Occurrence of Character in String
strspn -- Search Strings
wcscmp -- Compare Wide-Character Strings
wcsncmp -- Compare Wide-Character Strings
<string.h>