strcoll() — Compare Strings

Format

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

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_COLLATE category of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The strcoll() function compares two strings using the collating sequence that is specified by the program's locale.

Return Value

The strcoll() function returns a value indicating the relationship between the strings, as listed below:

Value Meaning
Less than 0 string1 less than string2
0 string1 equivalent to string2
Greater than 0 string1 greater than string2

If strcoll() is unsuccessful, errno is changed. The value of errno may be set to EINVAL (the string1 or string2 arguments contain characters that are not available in the current locale).

Example that uses strcoll()

This example compares the two strings that are passed to main() using strcoll():

#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 = strcoll( 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] );
  }
}
 
/******************  If the input is the strings  ***********************
****************  "firststring" and "secondstring",  ********************
******************  then the expected output is:  *****************
 
"firststring" is less than "secondstring"
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]