C Standard Library

C <wchar.h> - wcscoll() Function



The C <wchar.h> wcscoll() function is used to compare two null-terminated wide strings according to the current locale as defined by the LC_COLLATE category. The function starts comparing the first pair of wide characters of each wide string and continues comparing until the pair of wide characters are different or until a terminating null wide character is reached.

LC_COLLATE is used to select collation order of the C locale. The collation order is the dictionary order. Different languages around the world use different schemes for sorting or ordering textual values. For example, "ch" in Czech follows "h" and precedes "i", and "dzs" in Hungarian follows "dz" and precedes "g".

Syntax

int wcscoll (const wchar_t* lhs, const wchar_t* rhs);            

Parameters

lhs Specify pointer to the first null-terminated wide string to compare.
rhs Specify pointer to the second null-terminated wide string to compare.

Return Value

Based on the value of lhs and rhs, the function returns the following:

  • Negative value if lhs appears before rhs.
  • Zero if lhs and rhs compare equal.
  • Positive value if lhs appears after rhs.

Example: Value returned by function = 0

The example below shows the usage of wcscoll() function.

#include <stdio.h>
#include <locale.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"Hello";
  
  //using LC_COLLATE of Environment's 
  //default locale
  setlocale(LC_COLLATE,"");
  
  //comparing str1 and str2
  int retval = wcscoll(str1, str2);

  //displaying the result
  if(retval != 0)
    printf("str1 and str2 are not equal.\n");
  else
    printf("str1 and str2 are equal.\n");
 
  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

str1 and str2 are equal.
Value returned by the function: 0

Example: Value returned by function < 0

Lets consider this example where the returned value is less than zero.

#include <stdio.h>
#include <locale.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"World";
  
  //using LC_COLLATE of Environment's 
  //default locale
  setlocale(LC_COLLATE,"");
  
  //comparing str1 and str2
  int retval = wcscoll(str1, str2);

  //displaying the result
  if(retval != 0)
    printf("str1 and str2 are not equal.\n");
  else
    printf("str1 and str2 are equal.\n");

  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

str1 and str2 are not equal.
Value returned by the function: -15

Example: Value returned by function > 0

Lets consider another example where the returned value is greater than zero.

#include <stdio.h>
#include <locale.h>
#include <wchar.h>
 
int main (){
  wchar_t str1[20] = L"Hello";
  wchar_t str2[20] = L"Apple";

  //using LC_COLLATE of Environment's 
  //default locale
  setlocale(LC_COLLATE,"");

  //comparing str1 and str2
  int retval = wcscoll(str1, str2);

  //displaying the result
  if(retval != 0)
    printf("str1 and str2 are not equal.\n");
  else
    printf("str1 and str2 are equal.\n");

  printf("Value returned by the function: %d\n", retval);

  return 0;
}

The output of the above code will be:

str1 and str2 are not equal.
Value returned by the function: 7

❮ C <wchar.h> Library