wmemset() in C/C++ with Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

After copying G 10 times
GGGGGGGGGG

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

After copying r 0 times
Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Runtime Errors:
Segmentation Fault (SIGSEGV)


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.