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.

Return Value: The function returns three values as follows:

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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

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 :
C
Practice Tags :