vsprintf()

Writes formatted output to a string.

  #include <stdio.h>
    
  int vsprintf(char *s, const char *format, va_list arg);    
Parameter

s

A pointer to a character string in which to store the formatted text.

format

A pointer to a format string.

arg

An argument list.

Remarks

The vsprintf() function works identically to the sprintf() function. Instead of the variable list of arguments that can be passed to sprintf(), vsprintf() accepts its arguments in the array of type va_list processed by the va_start() macro from the stdarg.h header file.

Be careful when using this function. It can be a source for serious buffer overflow bugs. Unlike vsnprintf(), the programmer cannot specify a limit on the number of characters to store in s.

vsprintf() returns the number of characters written to s not counting the terminating null character. Otherwise, EOF on failure.

This facility may not be available on configurations of the EWL that run on platforms without file systems.

Listing: Example Of vsprintf() Usage

#include <stdarg.h>

#include <stdio.h>

int spr(char *, char *, ...);

int main(void)

{

int a = 56;

double x = 1.003;

static char name[] = "Charlie";

char s[50];

// format and send a variable number of arguments

// to character array s

spr(s, "%10s\n %f\n %-10d\n", name, x, a);

puts(s);

return 0;

}

// spr() formats and sends a variable number of

// arguments to a character array using the sprintf()

// function

int spr(char *s, char *format, ...)

{

va_list args;

int retval;

va_start(args, format); // prepare the arguments

retval = vsprintf(s, format, args);

va_end(args); // clean the stack

return retval;

}

Output:

Charlie

1.003000

56