What is wcsncat in C?

wcsncat in C appends the first specified number of wide characters from a source wide string to a destination wide string. The function is defined in the wchar.h header file and the function declaration in the standard C library is as follows:

wchar_t *wcsncat(wchar_t *dest, const wchar_t *src, size_t count);

If count is greater than the wide characters in src, the function stops at the null terminating character.

NOTE: A wide string uses characters from the Unicode character set, where typically each character is of 2 bytes.

Parameters

dest: destination wide string

src: source wide string

count: count of first wide characters to be appended

Return value

A copy of dest with appended count wide characters of src.

Example program

#include <wchar.h>
int main ()
{
wchar_t dest1[20] = L"How to use " ;
wchar_t dest2[20] = L"How to use " ;
wchar_t src[20] = L"wcsncat in C" ;
wcsncat(dest1, src, 7);
wcsncat(dest2, src, 12);
wprintf(L"%ls\n", dest1);
wprintf(L"%ls\n", dest2);
return 0;
}

Explanation

First, we import the wchar.h header file. In lines 5-7, we initialize the source and destination strings, where the L identifier before the string indicates that the characters to follow are from the Unicode character set. In line 8, we append the first 7 characters from the wide string wcsncat in C to dest1, which results in the final output How to use wcsncat.

In line 9, we append the first 12 characters from the wide string wcsncat in C to dest2, which results in the final output How to use wcsncat in C.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved