int snprintf ( char *str, size_t n,
const char *format, ... );
#include <stdarg.h>
int vsnprintf ( char *str, size_t n,
const char *format, va_list ap );
vsnprintf is the equivalent of snprintf with the variable argument list specified directly as for vprintf.
/* Construct a message describing the value of a
variable whose name is NAME and whose value is
VALUE. */
char *
make_message (char *name, char *value)
{
/* Guess we need no more than 100 chars of space. */
int size = 100;
char *buffer = (char *) xmalloc (size);
while (1)
{
/* Try to print in the allocated space. */
int nchars = snprintf (buffer, size,
"value of %s is %s", name, value);
/* If that worked, return the string. */
if (nchars > -1 && nchars < size)
return buffer;
/* Else try again with more space. */
if (nchars > -1)
size = nchars+1; /* precisely what is needed */
else
size *= 2; /* twice the old size */
buffer = (char *) xrealloc (buffer, size);
}
}