swprintf -- Format and Write Wide Characters to Buffer

Format

#include <wchar.h>
int swprintf(wchar_t *wcsbuffer, size_t n,
             const wchar_t *format, argument-list);

Language Level: ANSI 93
swprintf formats and stores a series of wide characters and values into the wide-character buffer wcsbuffer. swprintf is equivalent to sprintf, except that it operates on wide characters.

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

swprintf 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:

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

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.

In extended mode, swprintf also converts floating-point values of NaN and infinity to the strings "NAN" or "nan" and "INFINITY" or "infinity". The case and sign of the string is determined by the format specifiers.

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

Example
This example uses swprintf to format and print several values to buffer.

#include <wchar.h>
#include <stdio.h>
#define BUF_SIZE 100
int main(void)
{
   wchar_t wcsbuf[BUF_SIZE];
   wchar_t wstring[] = L"ABCDE";
   int     num;
   num = swprintf(wcsbuf, BUF_SIZE, L"%s", "xyz");
   num += swprintf(wcsbuf + num, BUF_SIZE - num, L"%ls", wstring);
   num += swprintf(wcsbuf + num, BUF_SIZE - num, L"%i", 100);
   printf("The array wcsbuf contains: \"%ls\"\n", wcsbuf);
   return 0;
   /***************************************************************
      The output should be similar to :
      The array wcsbuf contains: "xyzABCDE100"
   ***************************************************************/
}



printf -- Print Formatted Characters
sprintf -- Print Formatted Data to Buffer
swscanf -- Read Wide-Character Data from Buffer
vswprintf -- Format and Write Wide Characters to Buffer
<wchar.h>