Skip to content
Related Articles

Related Articles

Improve Article
wmemset() in C/C++ with Examples
  • Difficulty Level : Basic
  • Last Updated : 12 Sep, 2018

The wmemset() function is a builtin function in C/C++ which copies a single wide character for a specified number of time to a wide character array. It is defined within the cwchar header file in C++.

Syntax:

wmemset(des, ch, count)

Parameters: The function accepts three parameters which are described below.

  • des: It specifies to the wide character array to copy the wide character.
  • ch: It specifies to the wide character to copy.
  • count: It specifies to number of times to copy.

Return Value: The function returns three values as follows:

  • If the count is greater then 0, then function returns des.
  • If count is less than 0, Segmentation Fault may occur.
  • If count is equal to zero, the function does nothing.

Below programs illustrate the above function.
Program 1:






// C++ program to illustrate the
// wmemset() function when count is greater than 0
#include <cwchar>
#include <iostream>
using namespace std;
  
int main()
{
    wchar_t ch = L'G';
    wchar_t des[20];
    int count = 10;
  
    wmemset(des, ch, count);
    wcout << L"After copying " << ch << L" 10 times" << endl;
  
    for (int i = 0; i < count; i++)
        putwchar(des[i]);
  
    return 0;
}
Output:
After copying G 10 times
GGGGGGGGGG

Program 2:




// C++ program to illustrate the
// wmemset() function when count is 0
#include <cwchar>
#include <iostream>
using namespace std;
  
int main()
{
    wchar_t ch = L'r';
    wchar_t des[20];
    int count = 0;
  
    wmemset(des, ch, count);
    wcout << L"After copying " << ch << L" 0 times" << endl;
  
    for (int i = 0; i < count; i++)
        putwchar(des[i]);
  
    return 0;
}
Output:
After copying r 0 times
Program 3:




// C++ program to illustrate the
// wmemset() function when 
// count is less than 0
// returns a segmentation fault 
#include <cwchar>
#include <iostream>
using namespace std;
  
int main()
{
    wchar_t ch = L'q';
    wchar_t des[20];
    int count = -4;
  
    wmemset(des, ch, count);
    wcout << L"After copying " << ch << L" -4 times" << endl;
  
    for (int i = 0; i < count; i++)
        putwchar(des[i]);
  
    return 0;
}

Output:

Runtime Errors:
Segmentation Fault (SIGSEGV)
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :