Syntax
wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
The dest and src arguments are passed to the wcscpy() function. The wide-character string pointed to by src is copied to the memory location pointed to by dest. The wide character that terminates with a null is also copied.
If any of the following applies, the behaviour is undefined:
- The memory allotted for the dest pointer is insufficient.
- The strings cross each other.
Parameters
There are two following two parameters of wcscpy() function.
- dest: A wide-character array to which the contents will be copied.
-
src: A pointer to a large character array from which the contents will be copied.
Return Value: The modified destination is returned by the wcscpy() function.
Examples of wcscpy()
Example 1:
The following program demonstrates the above function. Here, we have a source string ‘Coding Ninjas’ and we are copying it into the destination string.
Code:
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.utf8");
//source string
wchar_t src[] = L"Coding Ninjas";
wchar_t dest[40];
wcout << L"Before copying the dest string is, dest = " << dest<<endl;
// Copying source to destination
wcscpy(dest,src);
wcout << L"After copying the dest string becomes, dest = " << dest;
return 0;
}
Output
Example 2:
Code:
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
wchar_t dest[40];
//Source String
wchar_t src[]=L"Coding Ninjas Studio Library is the blogs stream of Coding Ninjas.";
wcout << L"The Source String is: " << src << endl;
wcscpy(dest, src);
wcout << L"The Destination String after copying is: " << dest;
return 0;
}
Output:
You practice by yourself with the help of online c++ compiler.
Must Read Dynamic Binding in C++
Frequently Asked Questions
What exactly is Wcscpy?
In the cwchar.h header file, the wcscpy() function is defined. The wcscpy() function copies a long character string from one location to another.
What is a wchar_t in C++?
The wchar_t type is a wide-character type specified by the implementation. It's a 16-bit wide character in the Microsoft compiler that's used to store Unicode encoded as UTF-16LE, Windows' native character type.
How do I copy Tchar?
The wcscpy() method duplicates string2's contents (including the wchar t null character at the end) into string1. The wcscpy() method works with null-ended wchar t strings, therefore string parameters should include a wchar t null character to indicate the end of the string.
Conclusion
In this blog, we have extensively discussed the wcscpy() function in C++. We hope that this article has provided you with additional information about the C++ functions. And to learn in-depth about C++ functions, check out the course on our functions on the Coding Ninjas website. And also, check out the awesome content on the Coding Ninjas Website, Android Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, and Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.
Delighted Programming!