What is wcscoll in C?
The wcscoll function in C compares two strings in wide character format. The function can determine if the strings are equal or not.
To use the wcscoll function, the wchar.h library needs to be included in the program.
#include <wchar.h>
Syntax
int wcscoll (const wchar_t* string1, const wchar_t* string2);
Parameters
The wcscoll function in C takes in two parameters:
string1– The first string in wide character format.string2– The second string in wide character format.
The wcscoll function runs a comparison on these two strings.
Return value
The wcscoll function may return 0, a value greater than 0 or a value smaller than 0.
A value equal to zero denotes that the two strings are equal. A value greater than zero denotes that the strings are different, and the first different character has a larger value in string1. A return value smaller than 0 denotes that the first different character has a larger value in string2.
Example
The following snippet of code demonstrates how we may use the wcscoll function for string comparisons.
We call the wcscoll function thrice, each time with different input. Initially, the two strings passed as parameters to the wcscoll function are equal, so it returns 0, denoting that the two strings are equal. In the next 2 calls to wcscoll, the strings differ by one character. The first different character has a larger value that lies in string1 in the second function call, while in the third call, the first different character with a larger value lies in string2.
To print the results of the comparisons on the console, we pass the return value of each call to wcscoll to the check function, which uses if-else statements to determine the correct print statement:
#include <wchar.h>#include <stdio.h>int check(int ret){if (ret == 0){printf("The strings match!\n");}else if (ret > 0){printf("The strings do not match.\nThe first character that does not match has a greater value in the string passed as the second argument to wcscoll!\n");}else if (ret < 0){printf("The strings do not match.\nThe first character that does not match has a greater value in the string passed as the first argument to wcscoll!\n");}return 0;}int main(){//Strings are equalint ret = wcscoll(L"Educative", L"Educative");check(ret);//Value of first different character is larger in string 1ret = wcscoll(L"EDucative", L"Educative");check(ret);//Value of first different character is larger in string 2ret= wcscoll(L"Educative", L"EDucative");check(ret);return 0;}
Free Resources