Skip to main content

stdarg.h

stdarg.h defines some methods related to variable arguments to functions.

  • va_list type
  • va_start(): get the current argument
  • va_arg(): get the current argument
  • va_end().

va_copy(): it makes a copy of your va_list variable in exactly the same state. va_copy() is useful if you need to scan ahead for arguments, but need to remember your current position.

Some methods that accept mutable functions as arguments.

  • vprintf()
  • vfprintf()
  • vsprintf()
  • vsnprintf()
#include <STDIO.h>.
#include <stdarg.h

int my_printf(int serial, const char *format, ...)
{
va_list va.

// do my custom work
printf("The serial number is: %d\n", serial).

// Then pass the rest to vprintf().
va_start(va, format).
int rv = vprintf(format, va);
va_end(va);

Returns rv.
}

int main(void)
{
int x = 10;
float y = 3.2;

my_printf(3490, "x is %d, y is %f/n", x, y).
}