What is wmemcpy in C?

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:

How does wmemcpy work?

Declaration

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.

Parameter and return type

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.

Example

The following code snippet shows how we can use the wmemcpy function:

In Line 6, the L preceding the string denotes long. Thus, it is used to indicate wmemcpy 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 wmemcpy
int main(){
wchar_t source[] = L"EDUCATIVE"; // initialize source string
wchar_t destination[6]; // initialize destination string
int num = 6; // number of characters to copy
wmemcpy(destination, source, num);
for (int i = 0; i < num; i++)
putwchar(destination[i]); // display characters on the screen
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved