Wcrtomb Function In Cc++

The wcrtomb function in C/C++ transforms a wide character to its narrow multibyte representations. The wide-character wc is converted to its multibyte counterparts and saved in the array referenced by s . The method returns the length in bytes of the corresponding multibyte sequence pointed to s.

Syntax:

It has the following Syntax:

Example

size_t wcrtomb( char* st, wchar_t wcl, mbstate_t* pss )

Parameters: The function requires three necessary arguments, which are listed below:

  • st: It provides the reference to an array large enough to store a multibyte sequence.
  • wcl: It indicates the wide characters to convert.
  • pss: It define the conversion state used while reading the multibyte text.

Return values: The method returns the following values:

  • If it succeeds, it returns the number of bytes transferred to the character array whose initial element is referenced to by st.
  • If not, it returns -1 and changes errno to EILSEQ .

The wcrtombs function transforms a wide character, starting in the given conversion state stored in mbstate , from the numerical value contained in wcl to the location represented by mbchar . The pReturnValue value will represent the total amount of bytes converted, but not more than MBCUR_MAX bytes, or -1 if an error occurs.

If mbstate is empty, the internal mbstate_t conversion condition is utilized. If a single character in wcl does not have a matching multibyte character, the value of pReturnValue is -1, and the method returns the EILSEQ errno result.

Example:

Example

// C++ Program to implement the 
// wcrtomb() function in c++
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	setlocale(LC_ALL, "en_US.utf8"); 

	// string initialization
	wchar_t wcl[] = L"z\u00df\u6c34\U0001f34c"; 

	// array for storing the values
	char st[25]; 
	int returnValue; 

	// state
	mbstate_t pss= mbstate_t(); 
	for (int i = 0; i < wcslen(wcl); i++) { 
		returnValue = wcrtomb(st, wcl[i], &pss); 

		// display of the pointer size
		if (returnValue != -1) 
			cout << "Size of " << st << " is "
				<< returnValue << " bytes" << endl; 
		else
			cout << "Invalid wide character" << endl; 
	} 

	return 0; 
}

Output:

Output

Size of zU is 1 bytes
Invalid wide character
Invalid wide character
Invalid wide character

Input Required

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