vsprintf() — Print Argument Data to Buffer
Format
#include <stdarg.h>
#include <stdio.h>
int vsprintf(char *target-string, 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 vsprintf()
function
formats and stores a series of characters and values in the buffer target-string.
The vsprintf()
function
works just like the sprintf()
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 sprintf()
function
can have a list of arguments, but the number of arguments in that
list is fixed when you compile the program.
The vsprintf()
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
If successful, the vsprintf()
function
returns the number of bytes written to target-string.
If an error occurs, the vsprintf()
function
returns a negative value.
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);
vsprintf(string, fmt, arg_ptr);
va_end(arg_ptr);
}
/****************** Output should be similar to: ****************
The string is: Sat Sun Mon
*/
Related Information
- printf() — Print Formatted Characters
- sprintf() — Print Formatted Data to Buffer
- va_arg() – va_copy() – va_end() – va_start() — Handle Variable Argument List
- vfprintf() — Print Argument Data to Stream
- vprintf() — Print Argument Data
- vswprintf() — Format and Write Wide Characters to Buffer
- <stdarg.h>
- <stdio.h>