Reputation: 213
What is the difference between the following two variadic function definitions?
int f()
{
/* function definition */
}
int f(...)
{
/* function definition */
}
f()
is actually defined as a variadic function. I'm also assuming <stdarg.h>
can be included and used.
Upvotes: 2
Views: 107
Reputation: 123558
An empty parameter list in a function declaration indicates that the function takes an unspecified number of arguments (which is not the same as a variable number of arguments). An empty parameter list in a function definition (such as in the first definition of f
) indicates that the function takes no arguments. This is an obsolescent style and should not be used - to indicate that a function takes no parameters, use void
as the identifier list.
As of C89, a variadic function declarator must have at least one fixed parameter, followed by the ...
. So the second definition of f
won't work either.
int f( void ) // f takes no arguments
{
// do something
}
int f( T fixed, ... ) // one fixed parameter of some type, additional parameters as needed
{
// do something
}
printf
is as good an example of a variadic function in the standard library as any - its prototype is
int printf( const char *fmt, ... );
Upvotes: 5