vsprintf_s, vswprintf_s
Go Up to stdio.h Index
Header File
stdio.h
Category
Formatted input/output functions
Prototype
int vsprintf_s(char * restrict s, rsize_t n, const char * restrict format, va_list arg);
int vswprintf_s(wchar_t * restrict s, rsize_t n, const wchar_t * restrict format, va_list arg);
Description
Replaces vsprintf, vswprintf adding security enhancements.
The v...printf functions are known as alternate entry points for the ...printf functions. They behave exactly like their ...printf counterparts, but they accept a pointer to a list of arguments instead of an argument list.
vsprintf_s accepts a pointer to a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string. The number of format specifiers must be the same as the number of arguments.
vfprintf_s is equivalent to vsprintf, vswprintf, but it adds the parameter n and run-time constraints regarding the validity of s, format, and the items in arg corresponding to a %s format specifier (all mentioned earlier must not be null pointers.)
In case of a run-time constraint violation, vsprintf_s sets '\0' to s[0].
Unlike vsnprintf_s, vsnwprintf_s, vsprintf_s does not truncate the result to fit inside the array pointed to by s. Instead, a run-time constraint violation is issued.
Return Value
vsprintf_s returns the number of characters output. In case of an error, vsprintf_s returns a negative value. In any other case, vsprintf_s returns zero.
Example
#include <stdio.h>
#include <conio.h>
#include <stdarg.h>
char buffer[80];
int vspf(char *fmt, ...)
{
va_list argptr;
int cnt;
va_start(argptr, fmt);
cnt = vsprintf_s(buffer, 70,fmt, argptr);
va_end(argptr);
return(cnt);
}
int main(void)
{
int inumber = 30;
float fnumber = 90.0;
char string[4] = "abc";
vspf("%d %f %s", inumber, fnumber, string);
printf_s("%s\n", buffer);
return 0;
}
Portability
| POSIX | Win32 | ANSI C | ANSI C++ | |
|---|---|---|---|---|
|
vsprintf_s |
+ |
+ |
+ |
+ |
|
vswprintf_s |
+ |
+ |
+ |