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 );


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.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
    ptr = 0;// oops, I meant *ptr = 0

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q