Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Standard Library Header <cwchar>
3.
Syntax
4.
Parameters 
5.
Examples of wcscpy()
5.1.
Example 1:
5.1.1.
Code:
5.1.2.
Output
5.2.
Example 2:
5.2.1.
Code:
5.2.2.
Output:
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024

wcscpy() function in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In this blog, we will have a look at the wcscpy() function in C++. The C++ function wcscpy() copies a wide-character string from one location to another. To avoid overflows, the array pointed by destination must be large enough to hold the same C-wide string as the source (including the terminating null character) and must not overlap in memory with the source.

 

In the header file <cwchar>, the wcscpy() function is defined. The wcscpy() function copies a long character string from source to destination.

We'll go over the entire architecture and features of wcscpy() in this article.

Also see, Literals in C,Fibonacci Series in C++
 

Standard Library Header <cwchar>

This header was previously known as <wchar.h> in the C standard library. This header is included in the libraries of null-terminated wide and multibyte strings. It also includes some C-style I/O functions and C-style Date conversion.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview Experiences, and Coding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.

Delighted Programming!

Live masterclass