Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
copy

What is wmemset in C/C++?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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;
}

RELATED TAGS

c
copy

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring