Export (0) Print
Expand All
Expand Minimize

wcstombs

Converts a sequence of wide characters to a corresponding sequence of multibyte characters.

size_t wcstombs(
   char *mbstr,
   const wchar_t *wcstr,
   size_t count 
);

Parameters

mbstr
The address of a sequence of multibyte characters.
wcstr
The address of a sequence of wide characters.
count
The maximum number of bytes that can be stored in the multibyte output string.

Return Value

If wcstombs successfully converts the multibyte string, it returns the number of bytes written into the multibyte output string, excluding the terminating NULL (if any). If the mbstr argument is NULL, wcstombs returns the required size of the destination string. If wcstombs encounters a wide character it cannot be convert to a multibyte character, it returns –1 cast to type size_t.

Remarks

The wcstombs function converts the wide-character string pointed to by wcstr to the corresponding multibyte characters and stores the results in the mbstr array. The count parameter indicates the maximum number of bytes that can be stored in the multibyte output string (that is, the size of mbstr). In general, it is not known how many bytes will be required when converting a wide-character string. Some wide characters will require only one byte in the output string; others require two. If there are two bytes in the multibyte output string for every wide character in the input string (including the wide character NULL), the result is guaranteed to fit.

If wcstombs encounters the wide-character null character (L'\0') either before or when count occurs, it converts it to an 8-bit 0 and stops. Thus, the multibyte character string at mbstr is null-terminated only if wcstombs encounters a wide-character null character during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior of wcstombs is undefined.

If the mbstr argument is NULL, wcstombs returns the required size of the destination string.

Requirements

Routine Required header Compatibility
wcstombs <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

This program illustrates the behavior of the wcstombs function.

// crt_wcstombs.c
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   int      i;
   char    *pmbbuf   = (char *)malloc( 100 );
   wchar_t *pwchello = L"Hello, world.";

   printf( "Convert wide-character string:\n" );
   i = wcstombs( pmbbuf, pwchello, 100 );
   printf( "   Characters converted: %u\n", i );
   printf( "   Multibyte character: %s\n\n", pmbbuf );
}

Output

Convert wide-character string:
   Characters converted: 13
   Multibyte character: Hello, world.

See Also

Data Conversion Routines | Locale Routines | mblen | mbstowcs | mbtowc | wctomb | WideCharToMultiByte | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft