What is wcrtomb() in C ?

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>

Prototype

The function is written as shown below:

size_t wcrtomb( char *mbc, wchar_t wc, mbstate_t *state);

Parameters

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

Return value

Figure 1: Different return values of the wcrtomb function

Example

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 state
memset(&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

Copyright ©2025 Educative, Inc. All rights reserved