Format
#include <wcstr.h> wchar_t *wcsncpy(wchar_t *string1, const wchar_t *string2, size_t count);
Language Level: XPG4
wcsncpy copies up to count wide characters from string2
to string1. If string2 is shorter than count
characters, string1 is padded out to count
characters with wchar_t null characters.
wcsncpy 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
wcsncpy returns a pointer to string1.
Example
This example demonstrates the difference between wcscpy,
which copies the entire wide-character string, and wcsncpy, which
copies a specified number of wide characters from the string.
#include <stdio.h> #include <wcstr.h>
#define SIZE 40
int main(void)
{
wchar_t source[ SIZE ] = L"123456789";
wchar_t source1[ SIZE ] = L"123456789";
wchar_t destination[ SIZE ] = L"abcdefg";
wchar_t destination1[ SIZE ] = L"abcdefg";
wchar_t * return_string;
int index = 5;
/* This is how wcscpy works */ printf( "destination is originally = '%ls'\n", destination ); return_string = wcscpy( destination, source ); printf( "After wcscpy, destination becomes '%ls'\n\n", destination );
/* This is how wcsncpy works */ printf( "destination1 is originally = '%ls'\n", destination1 ); return_string = wcsncpy( destination1, source1, index ); printf( "After wcsncpy, destination1 becomes '%ls'\n", destination1 ); return 0;
/********************************************************************
The output should be:
destination is originally = 'abcdefg'
After wcscpy, destination becomes '123456789'
destination1 is originally = 'abcdefg'
After wcsncpy, destination1 becomes '12345fg'
********************************************************************/
}
![]()
strcpy -- Copy Strings
strncpy -- Copy Strings
wcscpy -- Copy Wide-Character Strings
wcsncat -- Concatenate Wide-Character
Strings
wcsncmp -- Compare Wide-Character
Strings
<wcstr.h>