What is wcscpy in C?
The wcscpy function in C copies a wide string to a specified location. wcscpy copies all characters prior to the null character.
The process is illustrated below:
Note: A wide string is comprised of characters from the Unicode character set.
To use the wcscpy function, you will need to include the <wchar.h> library in the program, as shown below:
#include <wchar.h>
The prototype of the wcscpy function is shown below:
wchar_t *wcscpy(wchar_t *dest, wchar_t *src);
Parameters
The wcscpy function takes the following two wchar_t* objects as parameters:
dest: destination stringsrc: null-terminated source string
Return Value
The wcscpy function copies the src string to the dest string and returns a pointer to the altered dest string.
The behavior of the wcscpy function is undefined in the following cases:
- The memory allocated for the
deststring is not large enough. - The
srcanddeststrings overlap.
Example
The code below shows how the wcscpy function works in C:
#include <stdio.h>#include <wchar.h>int main() {// initializing stringswchar_t src[] = L"Learning wcscpy with Educative!";wchar_t dst[40];// copying stringwcscpy(dst, src);// printing new dest stringprintf("%ls", dst);return 0;}
Explanation
First, two wide strings are declared for the source and destination addresses. The ‘L’ identifier in line 7 informs the compiler that the Unicode character set is being used.
The wcscpy function proceeds to copy the source string to the destination address in line 11. The updated value of the string is output.
Free Resources