wcsncpy() function in C++ with example

The wcsncpy() function is defined in cwchar.h header file. The wcsncpy() function copies a specified number of wide characters from source to destination.

Syntax:

wchar_t *wcsncpy(wchar_t *dest, 
                 const wchar_t *src, 
                 size_t n);

Parameters: This method accepts the following three parameters:

  • dest: specifies the pointer to the destination array.
  • src: specifies the pointer to the source array.
  • n: represents the number of character to copy.

Return Value: This function returns the modified destination.

Below programs illustrate the above function:-

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// c++ program to demonstrate
// example of wcsncpy() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the source string
    wchar_t src[] = L"A Computer Science portal for geeks";
  
    // maximum length of the destination string
    wchar_t dest[40];
  
    // copy the source to destination using wcsncpy
    wcsncpy(dest, src, 19);
  
    // Print the copied destination
    wcout << "Destination string is : " << dest;
  
    return 0;
}

chevron_right


Output:

Destination string is : A Computer Science

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c++ program to demonstrate
// example of wcsncpy() function.
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialize the source string
    wchar_t src[] = L"GeeksforGeeks";
  
    // maximum length of the destination string
    wchar_t dest[40];
  
    // copy the source to destination using wcsncpy
    wcsncpy(dest, src, 5);
  
    // Print the copied destination
    wcout << "Destination string is : " << dest;
  
    return 0;
}

chevron_right


Output:

Destination string is : Geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.