The wcscmp() function is defined in <cwchar> header file.
wcscmp() prototype
int wcscmp( const wchar_t* lhs, const wchar_t* rhs );
The wcscmp() function takes two arguments: lhs and rhs. It compares the contents of lhs and rhs lexicographically. The sign of the result is the sign of difference between the first pairs of characters that differ in lhs and rhs.
The behaviour of wcscmp() is undefined if either of lhs or rhs do not point to null terminated wide strings.
wcscmp() Parameters
- lhs: Pointer to the null terminated wide string to compare.
- rhs: Pointer to the null terminated wide string to compare.
wcscmp() Return value
The wcscmp() function returns a:
- positive value if the first differing character in lhs is greater than the corresponding character in rhs.
- negative value if the first differing character in lhs is less than the corresponding character in rhs.
- 0 if lhs and rhs are equal.
Example: How wcscmp() function works?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
void compare(wchar_t *lhs, wchar_t *rhs)
{
int result;
result = wcscmp(lhs, rhs);
if(result > 0)
wcout << rhs << " precedes " << lhs << endl;
else if (result < 0)
wcout << lhs << " precedes " << rhs << endl;
else
wcout << lhs << " and " << rhs << " are same" << endl;
}
int main()
{
setlocale(LC_ALL, "en_US.utf8");
wchar_t str1[] = L"\u0102\u0070ple";
wchar_t str2[] = L"\u00c4\u01f7ple";
wchar_t str3[] = L"\u00c4\u01a4ple";
compare(str1,str2);
compare(str2,str3);
return 0;
}
When you run the program, the output will be:
ÄǷple precedes Ăpple ÄƤple precedes ÄǷple