Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wmemcpy in C?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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;
}

RELATED TAGS

c

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring