vsprintf -- Write Formatted Output to a String

SYNOPSIS
#include <stdarg.h>
#include <stdio.h>
int vsprintf(char *s, const char *format, va_list arg);
DESCRIPTION
vsprintf
is equivalent to sprintf
with
arg
replacing the variable-argument list.
arg
has been initialized by the va_start
macro and possibly va_arg
calls. vsprintf
does not invoke the
va_end
macro. See va_arg
, va_end
, and va_start
for
details on varying-length argument-list functions.
RETURN VALUE
vsprintf
returns the number of characters written in the array, not
counting the terminating-null character.
EXAMPLE
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
static void format_msg(char *, int, int, ...);
#define NOTE 0
#define WARNING 1
main()
{
char msgbuf[100];
format_msg(msgbuf, NOTE, 0);
printf("The formatted message is: "%s" n", msgbuf);
format_msg(msgbuf, WARNING,1, "a replacement string");
printf("The formatted message is: "%s" n", msgbuf);
return;
}
static const char *msgs[] = {
"This is message number zero",
"This message requires %s"
};
static const char *levels[] = {
"NOTE: ",
"WARNING: "
};
static void format_msg(char *buf, int msgno, int level, ...)
{
va_list args;
va_start(args, level);
/* Copy in the message prefix. Format the */
/* remainder of the message with vsprintf(). */
strcpy(buf, levels[level]);
vsprintf(buf + strlen(levels[level]),msgs[msgno], args);
va_end(args);
}
RELATED FUNCTIONS
sprintf
, va_start
, vformat
, vsnprintf
SEE ALSO