Variadic Functions in C

#c

A variadic function is a fancy term for a function with a variable number of arguments. It will usually end in an ellipsis (…) as the last parameter of the function. In C you will mostly use a variadic function when you want to supply a string with format specifiers and a variable number of parameters those format specifiers translate to. Think of how sprintf works.

You can accomplish this in a few ways but I think the easiest way is with the va_start and va_end macros in combination with vsnprintf. The va macros will initialize and end a variable argument list, and vsnprintf will help write your formatted data to a buffer.

void myFunction (int param1, const char *format, ...) {
	static va_list args;
	static char buf[1000];

	va_start (args, format);
	vsnprintf(buf, sizeof(buf)-1, format, args);
	va_end (args);

	...
}