6.23.34 wcscmp Function
Compare two wide strings.
Include
<wchar.h>
Prototype
int wcscmp(const wchar_t * restrict s1, const wchar_t * restrict
s2);
Arguments
s1
- one wide string to compare
s2
- the other wide string to compare
Return Value
Returns a positive integer if s1
is greater than s2
, zero
if s1
is equal to s2
or a negative number if
s1
is less than s2
.
Remarks
The function returns a value based on the first wide character that differs between
s1
and s2
. Wide characters that follow a null wide
character are not compared.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <wchar.h>
int main(void)
{
wchar_t buf1[50] = L"Where is the time?";
wchar_t buf2[50] = L"Where did they go?";
wchar_t buf3[50] = L"Why?";
int res;
wprintf(L"buf1 : %ls\n", buf1);
wprintf(L"buf2 : %ls\n", buf2);
wprintf(L"buf3 : %ls\n\n", buf3);
res = wcscmp(buf1, buf2);
if (res < 0)
wprintf(L"buf1 comes before buf2\n");
else if (res == 0)
wprintf(L"buf1 and buf2 are equal\n");
else
wprintf(L"buf2 comes before buf1\n");
wprintf(L"\n");
res = wcscmp(buf1, buf3);
if (res < 0)
wprintf(L"buf1 comes before buf3\n");
else if (res == 0)
wprintf(L"buf1 and buf3 are equal\n");
else
wprintf(L"buf3 comes before buf1\n");
wprintf(L"\n");
res = wcscmp(L"Why?", buf3);
if (res < 0)
wprintf(L"\"Why?\" comes before buf3\n");
else if (res == 0)
wprintf(L"buf1 and \"Why?\" are equal\n");
else
wprintf(L"buf3 comes before \"Why?\"\n");
}
Example Output
buf1 : Where is the time?
buf2 : Where did they go?
buf3 : Why?
buf2 comes before buf1
buf1 comes before buf3
buf1 and "Why?" are equal