What is vswprintf() in C?

vswprintf() writes a wide character string to a wide character string buffer after formatting the data from the list of addresses passed as arguments.

Declaration

The declaration of vswprintf() is as follows:

int vswprintf(wchar_t * buffer, size_t bufferLen, const wchar_t * fmt,  va_list args);

Parameters

  • buffer: points to the wide character string that will store the formatted data.
  • bufferLen: specifies the length of buffer.
  • fmt: format specifiers of the input to be read.
  • args: points to the list of arguments in which the data will be stored.

Return value

If the write operation is successful, vswprintf() will return the number of wide characters written. On the other hand, if the write operation is not successful, vswprintf() will return a negative number.

Code

The code snippet below demonstrates the use of vswprintf():

#include <stdio.h>
#include <wchar.h>
#include <stdarg.h>
void writeFormatted(wchar_t * buffer, size_t bufferLen, const wchar_t * fmt, ... )
{
va_list args;
va_start(args, fmt);
vswprintf(buffer, bufferLen, fmt, args);
va_end(args);
}
int main ()
{
wchar_t str[] = L"%d mile is equal to %f %ls.\n";
int a = 1;
float x = 1.60934;
wchar_t wstr[] = L"kilometers";
wchar_t buffer[64];
writeFormatted(buffer, 64, str, a, x, wstr);
wprintf(L"%ls", buffer);
return 0;
}

Explanation

In line 24, when the wide character string and the variable arguments are passed to the function, va_list is created to store the variable arguments. va_list formats the wide character string based on the parameters following the string, and prints it in line 25.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved