wctomb, _wctomb_l

Convert a wide character to the corresponding multibyte character. More secure versions of these functions are available; see wctomb_s, _wctomb_s_l.


int wctomb(
   char *mbchar,
   wchar_t wchar
int _wctomb_l(
   char *mbchar,
   wchar_t wchar,
   _locale_t locale


The address of a multibyte character.

A wide character.

Return Value

If wctomb converts the wide character to a multibyte character, it returns the number of bytes (which is never greater than MB_CUR_MAX) in the wide character. If wchar is the wide-character null character (L'\0'), wctomb returns 1. If the target pointer mbchar is NULL, wctomb returns 0. If the conversion is not possible in the current locale, wctomb returns -1 and errno is set to EILSEQ.


The wctomb function converts its wchar argument to the corresponding multibyte character and stores the result at mbchar. You can call the function from any point in any program. wctomb uses the current locale for any locale-dependent behavior; _wctomb_l is identical to wctomb except that it uses the locale passed in instead. For more information, see Locale.

wctomb validates its parameters. If mbchar is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns -1.


Routine Required header
wctomb <stdlib.h>

For additional compatibility information, see Compatibility.


This program illustrates the behavior of the wctomb function.

// crt_wctomb.cpp
// compile with: /W3
#include <stdio.h>
#include <stdlib.h>

int main( void )
   int i;
   wchar_t wc = L'a';
   char *pmb = (char *)malloc( MB_CUR_MAX );

   printf( "Convert a wide character:\n" );
   i = wctomb( pmb, wc ); // C4996
   // Note: wctomb is deprecated; consider using wctomb_s
   printf( "   Characters converted: %u\n", i );
   printf( "   Multibyte character: %.1s\n\n", pmb );
Convert a wide character:
   Characters converted: 1
   Multibyte character: a

See also

Data Conversion
_mbclen, mblen, _mblen_l
mbstowcs, _mbstowcs_l
mbtowc, _mbtowc_l
wcstombs, _wcstombs_l