wctomb() — Convert wide character to multibyte character

Standards

Standards / Extensions C or C++ Dependencies

ISO C Amendment
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <stdlib.h>

int wctomb(char *string, wchar_t character);

General description

Converts the wchar_t value of character into a multibyte array pointed to by string. If the value of character is 0, the function is left in the initial shift state. At most, wctomb() stores MB_CUR_MAX characters in string.

The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. If you change the category, undefined results can occur.

Returned value

If successful, wctomb() returns the length in bytes of the multibyte character.

If character is not a valid multibyte character, wctomb() returns -1.

If string is a NULL pointer, wctomb() returns nonzero if shift-dependent encoding is used, or 0 otherwise.

Example

CELEBW30
⁄* CELEBW30                                      

   This example converts the wide character c to a character using wctomb().    

 *⁄                                                                             
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
                                                                                
#define SIZE 40                                                                 
                                                                                
int main(void)                                                                  
{                                                                               
  static char  buffer[ SIZE ];                                                  
  wchar_t wch = L'c';                                                           
  int length;                                                                   
                                                                                
  length = wctomb( buffer, wch );                                               
  printf( "The number of bytes that comprise the multibyte "                    
             "character is %i\n", length );                                     
  printf( "And the converted string is \"%s\"\n", buffer );                     
}                                                                               
Output:
The number of bytes that comprise the multibyte character is 1
And the converted string is "c"

Related information