w3resource

C wctomb() function

C wctomb() function - Convert a wide-character code to a character

Syntax wctomb() function

int wctomb(char *str, wchar_t wchar)

The wctomb() function is used to convert the wchar_t value of character into a multibyte array pointed to by string. The function is left in the initial shift state if the value of the character is 0.

Parameters wctomb() function

Name Description Required /Optional
str The address of a multibyte character. Required
wchar A wide character. Required

Return value from wctomb()

  • If wctomb converts the wide character to a multibyte character, it returns the number of bytes 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 isn't possible in the current locale, wctomb returns -1.

Example: wctomb() function

The following example shows the usage of wctomb() function.

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
 
#define SIZE 40
 
int main(void)
{
  static char buffer[ SIZE ];
  wchar_t wch = L'd';
  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 "d"

C Programming Code Editor:

Previous C Programming: C wcstombs()
Next C Programming: C string.h Home



Follow us on Facebook and Twitter for latest update.