2

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 "..."?

Abbas
  • 6,720
  • 4
  • 35
  • 49

3 Answers3

5

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.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • Wow. I did not know c had these, trying it out right now. Thanks. – Abbas Nov 14 '11 at 05:04
  • 1
    If you're trying to implement a variadic function function, you'll need to use `stdarg.h` to access the parameters. So it's not as straightforward as a normal function. – Mysticial Nov 14 '11 at 05:08
  • You are right, compared to other languages, it's not straight forward at all. It took me a couple of minutes to get a sum function write even with a Wikipedia sample. Anyways, good to know it's there. Something's better then nothing :). – Abbas Nov 14 '11 at 05:16
2

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).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

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 (...)

Sample Implementation:

#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.

talha2k
  • 24,937
  • 4
  • 62
  • 81
  • Please explain the lines `va_list ap;` and `va_end(ap);`. Thank you in advance. – wannik Nov 14 '11 at 05:46
  • 1
    va_list and va_end are the macros which are defined in stdarg.h library, va_list is used for argument pointer (ap) variables. while va_end(ap) ends the use of an argument pointer. After a va_end call, further va_arg calls with the same ap may not work. – talha2k Nov 14 '11 at 05:56