The wcrtomb
function converts a wide character to its narrow multi byte
character. The wchar.h
header needs to be included to use this function as shown below:
#include <wchar.h>
The function is written as shown below:
size_t wcrtomb( char *mbc, wchar_t wc, mbstate_t *state);
mbc
- the pointer to the character array that will store the multi byte
character.
wc
- the wide-character that is to be converted
state
- the pointer to the mbstate_t
object
The following code represents the use of the wcrtomb
function. Line 13 resets the state to the initial conversion state:
#include <string.h>#include <stdio.h>#include <wchar.h>int main( void ){size_t size = 0;mbstate_t state;char mbc = 0;wchar_t* wc = L"Q";//resetting initial conversion statememset(&state, 0, sizeof(state));size = wcrtomb(&mbc, *wc, &state);if (size > 0){printf("The corrsponding wide character \"");wprintf(L"%s\"", wc);printf(" was converted to \"%c\" ", mbc);printf("multibyte character.\n");}else //error condition{printf("No corresponding multibyte character ""was found.\n");}}
Free Resources