Open In App

wmemset() in C/C++ with Examples

Last Updated : 15 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 than 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

CPP




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

CPP




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

CPP




// 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)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads