Skip to content
Related Articles

Related Articles

Improve Article

wcsncpy() function in C++ with example

  • Last Updated : 05 Oct, 2018
Geek Week

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:




// 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;
}
Output:
Destination string is : A Computer Science

Example 2:-




// 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;
}
Output:
Destination string is : Geeks
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :