C library function - wcstombs()


The C library function size_t wcstombs(char *str, const wchar_t *pwcs, size_t n) converts the wide-character string pwcs to a multibyte string starting at str. At most n bytes are written to str.


Following is the declaration for wcstombs() function.

size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)


  • str − This is the pointer to an array of char elements at least n bytes long.

  • pwcs − This is wide-character string to be converted.

  • n − This is the maximum number of bytes to be written to str.

Return Value

This function returns the number of bytes (not characters) converted and written to str, excluding the ending null-character. If an invalid multibyte character is encountered, -1 value is returned.


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

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 50

int main () {
   size_t ret;
   char *MB = (char *)malloc( BUFFER_SIZE );
   wchar_t *WC = L"https://www.tutorialspoint.com";

   /* converting wide-character string */
   ret = wcstombs(MB, WC, BUFFER_SIZE);
   printf("Characters converted = %u\n", ret);
   printf("Multibyte character = %s\n\n", MB);

Let us compile and run the above program that will produce the following result −

Characters converted = 29
Multibyte character = https://www.tutorialspoint.com