The wmemcpy
function is defined in the <wchar.h>
header file in C. It is used to copy a given number of characters from one string to another. It takes in three parameters: a source string
, destination string
, and number of characters
to copy. It then returns the destination string.
The illustration below shows how wmemcpy
function works:
The wmemcpy
function is defined as following:
wchar_t* wmemcpy( wchar_t* destination, const wchar_t* source,
size_t n )
It returns the destination
string.
wchar_t
stands for wide character. It is similar to char
except char
assigns one byte to each character, whereas wchar_t
assigns two bytes. The source
is of type const wchar_t
since it is initialized beforehand. The *
refers to a pointer to an array.
The return type is wchar_t*
, which is the same as destination
.
The following code snippet shows how we can use the wmemcpy
function:
In
Line 6
, theL
preceding the string denoteslong
. Thus, it is used to indicatewmemcpy
that a complete string is to be stored instead of a single character.
#include <stdio.h> // include header for putwchar#include <wchar.h> // include header for wmemcpyint main(){wchar_t source[] = L"EDUCATIVE"; // initialize source stringwchar_t destination[6]; // initialize destination stringint num = 6; // number of characters to copywmemcpy(destination, source, num);for (int i = 0; i < num; i++)putwchar(destination[i]); // display characters on the screenreturn 0;}
Free Resources