Variadic Functions
Variadic functions are functions which can take in a variable number of arguments. The C language by itself provides no special syntax for dealing with them, but variadic functions are supported by the standard C library (<varargs.h>
).
Syntax
A variadic function is declared as:
Where MyFunction is defined as a function which takes 1 required argument, and as many non-optional arguments as you want (up to the practical limit set by the maximum number of function arguments you are allowed).
A variadic function is defined as:
But how do you access the variable arguments in your function definition? Use the function-like macros va_start
, va_arg
and va_end
provided by <varargs.h>
.
The second argument passed to va_arg
, type, must be a self-promoting type.
For most compilers, va_end does nothing (including the GCC compiler). But you should always include it for portability reasons.