Open In App

wcsncpy() function in C++ with example

Last Updated : 05 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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: cpp
// 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:- cpp
// 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

Article Tags :
Practice Tags :

Similar Reads