vsnprintf() — Print Argument Data to Buffer
Format
#include <stdarg.h>
#include <stdio.h>
int vsnprintf(char *target-string, size_t n, const char *format, va_list arg_ptr);
Language Level
ANSI
Threadsafe
Yes
Locale Sensitive
The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. The behavior might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.
Description
The vsnprintf(
)
function formats and stores a series of characters and values in the
buffer target-string. The vsnprintf()
function works
just like the snprintf()
function, except that arg_ptr points
to a list of arguments whose number can vary from call to call in
the program. These arguments should be initialized by the va_start
function
for each call. In contrast, the snprintf()
function
can have a list of arguments, but the number of arguments in that
list is fixed when you compile the program.
The vsnprintf()
function converts each
entry in the argument list according to the corresponding format specifier
in format. The format has the same form and function
as the format string for the printf()
function.
Return Value
The vsnprintf()
function
returns the number of bytes that are written in the array, not counting
the ending null character.
Example
#include <stdarg.h>
#include <stdio.h>
void vout(char *string, char *fmt, ...);
char fmt1 [] = "%s %s %s\n";
int main(void)
{
char string[100];
vout(string, fmt1, "Sat", "Sun", "Mon");
printf("The string is: %s\n", string);
}
void vout(char *string, char *fmt, ...)
{
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(string, 8, fmt, arg_ptr);
va_end(arg_ptr);
}
/****************** Output should be similar to: ****************
The string is: Sat Su
*/
Related Information
- printf() — Print Formatted Characters
- sprintf() — Print Formatted Data to Buffer
- snprintf() — Print Formatted Data to Buffer
- va_arg() – va_copy() – va_end() – va_start() — Handle Variable Argument List
- vfprintf() — Print Argument Data to Stream
- vsprintf() — Print Argument Data to Buffer
- <stdarg.h>
- <stdio.h>