Reputation: 6886
I was going through the stdio.h header file that comes with MinGW and noticed that the printf
function is declared like this:
int printf (const char *__format, ...)
{
//body omitted
}
I have never seen ellipsis in function parameter list before so I tried it out. It compiles and runs without error. What, then, is the purpose of "..."?
Upvotes: 2
Views: 173
Reputation: 25473
it is used to allow a variable number of arguments or parameters of unspecified type, as printf()
do. the function which allows variable number of argumnets is called Variadic Function
Variadic Variables are accessed with va_start
, va_list
, va_end
and va_arg
Variable number of Arguments (...)
#include <stdarg.h>
double average(int count, ...)
{
va_list ap;
int j;
double tot = 0;
va_start(ap, count); //Requires the last fixed parameter (to get the address)
for(j=0; j<count; j++)
tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
va_end(ap);
return tot/count;
}
Hope this helps.
Upvotes: 1
Reputation: 597051
It informs the compiler that the function has a variadic list of parameters. It is a feature that only works with the __cdecl
calling convention. It allows the caller to specify whatever parameter values it wants after the last fixed parameter, as the caller will clean up the parameters when the function exits. Variadic parameters are commonly used for printf-style functions where the interpretation of the variadic parameter values is dependant on the value of the fixed parameter values (such as matching up individual variadic parameters to each format specifier in a __format
parameter).
Upvotes: 2
Reputation: 471369
That means that the function is a variadic function that takes a variable number of parameters:
http://en.wikipedia.org/wiki/Variadic_function
printf()
itself is probably the best example of a variadic function.
Upvotes: 5