Format
#include <wcstr.h> int wcsncmp(const wchar_t *string1, const wchar_t *string2, size_t count);
Language Level: XPG4
wcsncmp compares up to count wide characters in string1
to string2.
wcsncmp operates on null-terminated wide-character strings; string arguments to this function should contain a wchar_t null character marking the end of the string.
Return Value
wcsncmp returns a value indicating the relationship
between the two strings, as follows:
| Value | Meaning |
| Less than 0 | string1 less than string2 |
| 0 | string1 identical to string2 |
| Greater than 0 | string1 greater than string2. |
Example
This example demonstrates the difference between wcscmp,
which compares the entire strings, and wcsncmp, which compares
only a specified number of wide characters in the strings.
#include <stdio.h> #include <wcstr.h>
#define SIZE 10
int main(void)
{
int result;
int index = 3;
wchar_t buffer1[SIZE] = L"abcdefg";
wchar_t buffer2[SIZE] = L"abcfg";
void print_result( int, wchar_t *, wchar_t * );
result = wcscmp( buffer1, buffer2 ); printf( "Comparison of each character\n" ); printf( " wcscmp: " ); print_result( result, buffer1, buffer2 );
result = wcsncmp( buffer1, buffer2, index); printf( "\nComparison of only the first %i characters\n", index ); printf( " wcsncmp: " ); print_result( result, buffer1, buffer2 ); return 0;
/*********************************************************************
The output should be:
Comparison of each character
wcscmp: "abcdefg" is less than "abcfg"
Comparison of only the first 3 characters
wcsncmp: "abcdefg" is identical to "abcfg"
*********************************************************************/
}
void print_result( int res, wchar_t * p_buffer1, wchar_t * p_buffer2 )
{
if ( res == 0 )
printf( "\"%ls\" is identical to \"%ls\"\n", p_buffer1, p_buffer2);
else if ( res < 0 )
printf( "\"%ls\" is less than \"%ls\"\n", p_buffer1, p_buffer2 );
else
printf( "\"%ls\" is greater than \"%ls\"\n", p_buffer1, p_buffer2 );
}
![]()
strncmp -- Compare Strings
strcmp -- Compare Strings
strcoll -- Compare Strings
strcmpi -- Compare Strings Without
Case Sensitivity
stricmp -- Compare Strings as
Lowercase
strnicmp -- Compare Strings Without
Case Sensitivity
wcscmp -- Compare Wide-Character
Strings
wcsncat -- Concatenate Wide-Character
Strings
wcsncpy -- Copy Wide-Character Strings
<wcstr.h>