Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is vswprintf() in C?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

c

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring