6.23.41 wcsncmp Function
Compare two wide strings.
Include
<wchar.h>
Prototype
int wcsncmp(const wchar_t * restrict s1, const wchar_t * restrict s2,
size_t n);
Arguments
s1
- one wide string to compare
s2
- the other wide string to compare
n
- the maximum number of wide characters 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
after at most n
comparisons.
Wide characters that follow a null wide character are not compared.
Example
#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 = wcsncmp(buf1, buf2, 6);
if (res < 0)
wprintf(L"buf1 comes before buf2\n");
else if (res == 0)
wprintf(L"6 characters of buf1 and buf2 are equal\n");
else
wprintf(L"buf2 comes before buf1\n");
wprintf(L"\n");
res = wcsncmp(buf1, buf2, 20);
if (res < 0)
wprintf(L"buf1 comes before buf2\n");
else if (res == 0)
wprintf(L"20 characters of buf1 and buf2 are equal\n");
else
wprintf(L"buf2 comes before buf1\n");
wprintf(L"\n");
res = wcsncmp(buf1, buf3, 20);
if (res < 0)
wprintf(L"buf1 comes before buf3\n");
else if (res == 0)
wprintf(L"20 characters of buf1 and buf3 are equal\n");
else
wprintf(L"buf3 comes before buf1\n");
}
Example Output
buf1 : Where is the time?
buf2 : Where did they go?
buf3 : Why?
6 characters of buf1 and buf2 are equal
buf2 comes before buf1
buf1 comes before buf3