vswprintf -- Format and Write Wide Characters to Buffer

Format

#include <stdarg.h>
#include <wchar.h>
int vswprintf(wchar_t *wcsbuffer, size_t n,
              const wchar_t *format, va_list argptr);

Language Level: ANSI 93
vswprintf formats and stores a series of wide characters and values in the buffer wcsbuffer. vswprintf works just like swprintf, except that argptr points to a list of wide-character arguments whose number can vary from call to call. These arguments should be initialized by va_start for each call. In contrast, swprintf can have a list of arguments, but the number of arguments in that list are fixed when you compile in the program.

The value n specifies the maximum number of wide characters to be written, including the terminating null character.

vswprintf converts each entry in the argument list according to the corresponding wide-character format specifier in format. The format has the same form and function as the format string for printf, with the following exceptions:

For a complete description of format specifiers, see printf -- Print Formatted Characters.

A null wide character is added to the end of the wide characters written; the null wide character is not counted as part of the returned value. If copying takes place between objects that overlap, the behavior is undefined.

If you specify a null string for the %s or %ls format specifier, vswprintf prints (null).

Return Value
vswprintf returns the number of bytes written in the array, not counting the terminating null wide character.

Example
This example creates a function vout that takes a variable number of wide-character arguments and uses vswprintf to print them to wcstr.

#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
wchar_t *format3 = L"%ls  %d  %ls";
wchar_t *format5 = L"%ls  %d  %ls  %d  %ls";
void vout(wchar_t *wcs, size_t n, wchar_t *fmt, ...)
{
   va_list arg_ptr;
   va_start(arg_ptr, fmt);
   vswprintf(wcs, n, fmt, arg_ptr);
   va_end(arg_ptr);
   return;
}
int main(void)
{
   wchar_t wcstr[100];
   vout(wcstr, 100, format3, L"ONE", 2L, L"THREE");
   printf("%ls\n", wcstr);
   vout(wcstr, 100, format5, L"ONE", 2L, L"THREE", 4L, L"FIVE");
   printf("%ls\n", wcstr);
   return 0;
   /************************************************************
      The output should be similar to:
      ONE  2  THREE
      ONE  2  THREE  4  FIVE
   ************************************************************/
}



printf -- Print Formatted Characters
swprintf -- Format and Write Wide Characters to Buffer
vfprintf -- Print Argument Data to Stream
vprintf -- Print Argument Data
vsprintf -- Print Argument Data to Buffer
<stdarg.h>
<wchar.h>