Open In App

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




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

Article Tags :