wcsrtombs() function in C/C++

The wcsrtombs() function converts wide character to multibyte string. This function converts the wide character string represented by *src to corresponding multibyte character string and is stored in the character array pointed to by dest if dest is not null. A maximum of len characters are written to dest.

Syntax:

size_t wcsrtombs( char* dest, const wchar_t** src, size_t len, mbstate_t* ps )

Parameters: The function accepts four mandatory parameters which are described below:

Return value : The function returns two value as below:



Below programs illustrate the above function:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcsrtombs() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize the string
    const wchar_t* src = L"Geekforgeeks";
  
    // maximum length of the dest
    char dest[20];
  
    // intial state
    mbstate_t ps = mbstate_t();
  
    // maximum length of multibyte character
    int len = 12;
  
    // function to convert wide-character
    // to multibyte string
    int value = wcsrtombs(dest, &src, len, &ps);
  
    cout << "Number of multibyte characters = " << value << endl;
  
    cout << "Multibyte characters written = " << dest << endl;
  
    return 0;
}
chevron_right

Output:
Number of multibyte characters = 12
Multibyte characters written = Geekforgeeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// wcsrtombs() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // initialize the string
    const wchar_t* src = L"This website is the best";
  
    // maximum length of the dest
    char dest[20];
  
    // intial state
    mbstate_t ps = mbstate_t();
  
    // maximum length of multibyte character
    int len = 14;
  
    // function to convert wide-character
    // to multibyte string
    int value = wcsrtombs(dest, &src, len, &ps);
  
    cout << "Number of multibyte characters = " << value << endl;
  
    cout << "Multibyte characters written = " << dest << endl;
  
    return 0;
}
chevron_right

Output:
Number of multibyte characters = 14
Multibyte characters written = This website i

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





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 :
C
Practice Tags :