Wcsncpy Function In C++ - C++ Programming Tutorial
C++ Course / Functions / Wcsncpy Function In C++

Wcsncpy Function In C++

BLUF: Mastering Wcsncpy Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Wcsncpy Function In C++

C++ is renowned for its efficiency. Learn how Wcsncpy Function In C++ enables low-level control and high-performance computing in the tutorial below.

The wcsncpy function is specifically a function within the C Standard Library and is not included in C++. The prefix "wcs" denotes "wide character string", signifying its role in managing wide character strings commonly employed in internationalization and Unicode support. Now, let's delve deeper into the details of wcsncpy.

Syntax:

It has the following syntax:

Copy at most num characters from source to destination, both of which are wide-character strings.

Destination: A pointer pointing to the destination array where the data will be duplicated.

A reference to the origin of the data to be duplicated.

Num: The maximum count of wide characters to duplicate.

Behavior:

  • The wcsncpy function copies the string pointed to by source to the array pointed to by destination, copying at most num wide characters.
  • Null wide characters (L'\0') are added to the destination array until num characters are written in total if the length of the source string is less than num.
  • Wcsncpy will not null-terminate the destination string if the length of the source string is greater than or equal to num.

If the value of num is equal to or exceeds the length of the original string, the wcsncpy function does not ensure that the destination string will be null-terminated. To ensure null-termination, you might have to manually append the null character to the destination array after employing wcsncpy.

Example 1:

Let's consider a scenario to demonstrate the application of the wcsncpy function in the C++ programming language.

Example

#include <iostream>
#include <cwchar>
using namespace std;
int main() {
const wchar_t* source = L"Java Cpp Tutorial";
wchar_t destination[20];
wcsncpy(destination, source, 10);
destination[10] = L'\0';
wcout << destination <<endl;
return 0;
}

Output:

Explanation:

In this instance, the target array is deliberately marked as null-terminated following the execution of the wcsncpy function, which duplicates up to a maximum of ten wide characters from the origin string to the target array. Subsequently, the result is displayed using std::wcout.

Example 2:

Let's consider another instance to demonstrate the functionality of the wcsncpy method in C++.

Example

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
wchar_t src[] = L"Java Cpp Tutorial is my favorite website"; 
wchar_t dest[40]; 
wcsncpy(dest, src, 19); 
wcout << "Destination string is : " << dest; 
return 0; 
}

Output:

Example 3:

Let's consider an example to demonstrate the application of the wcsncpy function in C++.

Example

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
wchar_t src[] = L"Java Cpp Tutorial"; 
wchar_t dest[40]; 
wcsncpy(dest, src, 5); 
wcout << "Destination string is : " << dest; 
return 0; 
}

Output:

Example 4:

Let's consider a scenario to demonstrate the functionality of the wcsncpy method in C++.

Example

#include <wchar.h>
int main ()
{
wchar_t wcs1[] = L"I like java cpp tutorial";
wchar_t wcs2[40];
wchar_t wcs3[40];
wcsncpy ( wcs2, wcs1, 40 );
wcsncpy ( wcs3, wcs2, 6 );
wcs3[6] = L'\0'; 
wprintf (L"%ls\n%ls\n%ls\n",wcs1,wcs2,wcs3);
return 0;
}

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience