strcmp() — Compare strings

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <string.h>

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

General description

The strcmp() built-in function compares the string pointed to by string1 to the string pointed to by string2 The string arguments to the function must contain a NULL character (\0) marking the end of the string.

The relation between the strings is determined by subtracting: string1[i] - string2[i], as i increases from 0 to strlen of the smaller string. The sign of a nonzero return value is determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared. This function is not locale-sensitive.

Returned value

strcmp() returns a value indicating the relationship between the strings, as listed below.
Value
Meaning
< 0
String pointed to by string1 less than string pointed to by string2
= 0
String pointed to by string1 equivalent to string pointed to by string2
> 0
String pointed to by string1 greater than string pointed to by string2

Example

CELEBS36
⁄* CELEBS36                                      

   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] );            
  }                                                                             
}                                                                               

Output

If the input is the strings "is this first?" and "is this before that one?" then the expected output is:
"is this first?" is greater than "is this before that one?"

Related information