c

Function in C with unlimited/variable number of arguments?


How do I define a function in C (specifically C89/C90, C99 and newer) which can take a variable number of arguments of any datatype. The same way as functions like : printf(), scanf(), etc.


Solution

  • To make a function accept a variable number of arguments in C you need to include the header

    #include <stdarg.h>
    

    printf() in C is an example of a variadic function that takes variable number of arguments.

    int printf(const char *fmt, ...)
    

    More info here and see also the wiki for stdarg.h