Wcrtomb Function In Cc++ - C++ Programming Tutorial
C++ Course / Functions / Wcrtomb Function In Cc++

Wcrtomb Function In Cc++

BLUF: Mastering Wcrtomb Function In Cc++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Wcrtomb Function In Cc++

C++ is renowned for its efficiency. Learn how Wcrtomb Function In Cc++ enables low-level control and high-performance computing in the tutorial below.

The wcrtomb function in C/C++ converts a wide character into its corresponding narrow multibyte forms. It takes a wide-character wc, converts it into its multibyte equivalents, and stores them in the array pointed to by s. This function also indicates the byte length of the resulting multibyte sequence stored in 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 function provides the following outcomes:

  • In case of success, it delivers the count of bytes moved to the character array pointed to by st.
  • If unsuccessful, it yields -1 and updates errno to EILSEQ.

The wcrtombs function converts a wide character, beginning in the provided conversion state saved in mbstate, from the numeric value in wcl to the position indicated by mbchar. The pReturnValue variable will indicate the total number of bytes converted, up to a maximum of MBCUR_MAX bytes, or -1 in case of an error.

If the mbstate is devoid of content, the internal conversion condition of mbstate_t is employed. In scenarios where a solitary character within wcl lacks a corresponding multibyte character, the pReturnValue will be set to -1, triggering the method to yield the EILSEQ errno outcome.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience