What is wmemset in C/C++?

The wmemset() function in C copies a single wide character for a specified number of times to a wide-character array.

Wide characters are similar to character datatype. The main difference between the two is that char takes 1-byte space, while the wide-character takes 2-bytes of space in memory (sometimes 4-byte, depending on compiler).

For 2-byte space, wide characters can hold 64K (65536) different characters. The wide char can hold UNICODE characters.

Standard library

#include <cwchar>

Prototype

wchar_t* wmemset( wchar_t* destination, wchar_t ch, size_t count );

The wmemset() function takes three arguments:

  1. destination

  2. ch

  3. count

The wide-character represented by ch is copied into the first count characters of the wide-character array pointed to by dest.

Parameters

destination: Pointer to the wide-character array to copy the wide-character.

ch: The wide character to copy

count: Number of times to copy

Code

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
wchar_t ch = 'B'; //Input
wchar_t dest[20]; //Array to Store Data
int count = 5; //defines how many times you have to iterate
wmemset(dest, ch, count); //pass variables to function
wcout << "After copying " << ch << " 5 times" << endl;
for(int i = 0; i < count; i++)
putwchar(dest[i]); //store the data in array
return 0;
}
Copyright ©2024 Educative, Inc. All rights reserved