Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

wcsncpy — bounded wide-character copying

wcsncpy copies wchar_t elements, padding short results with zeros but not guaranteeing string termination.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <wchar.h>
wchar_t *wcsncpy(wchar_t * restrict s1, const wchar_t * restrict s2, size_t n);

wcsncpy from <wchar.h> copies from s2 to s1. The limit n counts wchar_t elements, not bytes. It returns s1.

A shorter source triggers padding with L'\0' until exactly n elements are written. If the first n elements contain no zero, the result has no terminator.

The destination must hold n elements; the source must be readable through its first zero or for n elements. Overlapping copied regions cause undefined behavior. The limit does not check destination capacity.

Examples

#include <stdio.h>
#include <wchar.h>

int main(void)
{
    wchar_t padded[4];
    wchar_t cut[2];

    wcsncpy(padded, L"AB", 4);
    wcsncpy(cut, L"ABC", 2);

    printf("padding: %d %d\n",
           padded[2] == L'\0', padded[3] == L'\0');
    printf("prefix: %d %d\n", cut[0] == L'A', cut[1] == L'B');
    return 0;
}

Result

padding: 1 1
prefix: 1 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources