Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is vsnprintf_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.

Answers Code

vsnprintf_s() prints a formatted list of arguments to a character array. The vsnprint_s() function validates the arguments at runtime before printing them. The declaration for vsnprintf_s() is shown below:

int vsnprintf_s(char *arr, size_t len, const wchar_t *format, va_list args);

Parameters

  • arr: Pointer to the character array where output is to be printed.
  • len: Maximum number of characters that can be written to the array.
  • format: Format in which the output will be printed.
  • args: Pointer to the list of arguments to be printed.

Return value

If the write operation to the output stream is successful, vsnprintf_s() returns the number of characters written. A negative value is returned if there is an error when writing to the output stream.

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

Example

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

#include <stdio.h>
#include <stdarg.h>
int writeformatted(char* buffer, int bufferSize, const char *format, ...)
{
int len = 0;
va_list arguments;
va_start(arguments, format);
len = vsnprintf_s(buffer, bufferSize, format, arguments);
va_end(arguments);
return len;
}
int main ()
{
char buf[50];
char s1[50] = "vsnprintf() Example Code";
int len = writeformatted(buf, 50, "%s ", s1);
printf("Content of array: %s \n", buf);
printf("No. of characters in array: %d \n", len);
return 0;
}

Output

Content of array: vsnprintf() Example Code  
No. of characters in array: 25 

Explanation

The writeformatted() function in line 19 uses vsnprintf_s() in line 9 to print s1 in buf.

Note: The function above is not supported by the GCC compiler, hence you will get an implicit declaration of function vsnprintf_s error. Use the following variant to get the job done: int vsnprintf(char *arr, size_t len, 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.

Answers Code
Keep Exploring