C++ wcsncat()

The wcsncat() function in C++ appends a specified number of wide characters of a wide string to the end of another wide string.

The wcsncat() is defined in <cwchar> header file.

wcsncat() prototype

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

The wcsncat() function takes three arguments: dest, src and count. This function appends a maximum of count wide characters of the wide string pointed to by src the end of wide string pointed to by dest. The null terminating wide character at the end of dest is replaced by the first wide character of src and the resulting wide string is also null terminated.

The behaviour is undefined if

  • the strings overlap.
  • the dest array is not large enough to append the contents of src.

wcsncat() Parameters

  • dest: Pointer to a null terminating wide string to append to.
  • src: Pointer to a null terminating wide string that is to be appended.
  • count: Maximum numbers of wide characters to copy.

wcsncat() Return value

  • The wcsncat() function returns dest.

Example: How wcsncat() function works?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t src[] = L"\u0410\u0411\u0412\u0413\u0415\u0416\u0417\u0418";
	wchar_t dest[] = L"\u0424\u0425\u0426\u0427\u0428\u0429";
	
	wcout << L"Before appending, dest = " << dest << endl;
	wcsncat(dest, src, 4);
	
	wcout << L"After appending, dest = " << dest << endl;
	return 0;
}

When you run the program, the output will be:

Before appending, dest = ФХЦЧШЩ
After appending, dest = ФХЦЧШЩАБВГ