Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is vwprintf_s() in C?

Kainat Asif

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.

vwprintf_s() prints a formatted list of arguments to stdout; vwprint_s() validates the arguments at runtime before printing them. The declaration for vwprintf_s() is shown below:

int vswprintf_s( const wchar_t *format, va_list args);

Note: The vwprintf_s function is not supported by some compilers. If a compiler supports vwprintf_s, the value of STDC_WANT_LIB_EXT1 is set to 1.

Parameters

  • format: Format in which the output will be printed.
  • args: Pointer to the list of arguments to be printed.

Return value

The vwprintf_s() function returns the number of characters written to stdout if the data is written successfully. A negative value is returned if there is an error when writing.

Note: The return value of vwprintf_s() does not count terminating null characters.

Example

Consider the code snippet below, which demonstrates the use of vwprintf_s():

#include <stdio.h>
#include <wchar.h>
#include <stdarg.h>

int writeformatted (const wchar_t * format, ...)
{
  int len = 0;
  va_list arg;
  va_start (arg, format);
  len = vwprintf_s (format, arg);
  va_end (arg);
  return len;
}

int main ()
{

   int len = writeformatted( L"vwprintfExample: %s\n", "writing to stdout");

   writeformatted( L"Number of character written to stdout: %d",len);

   return 0;
}

Output

vwprintfExample: writing to stdout
Number of characters written to stdout: 35

Explanation

The writeformatted() function is used in line 18, which uses vwprintf_s() in line 10 to write to stdout.

Note: The function above is not supported by the GCC compiler, hence you will get an implicit declaration of function vwprintf_s error. Use the following variant to get the job done: int vswprintf_s( const wchar_t *format, va_list args);.

RELATED TAGS

c

CONTRIBUTOR

Kainat Asif
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