variadic functions and variadic macros

this is a draft for now.

A function that takes variable number of argument is called a "variadic function". Below is a few examples:

void a(int a, ...)

void a(int a, int b, ...)

Note that there must be at least one fixed argument before "ellipsis". Below prototype is illegal:

void a(...)

The way to use variadic is generally straight forward. I found below link is the most detailed on

https://www.gnu.org/software/libc/manual/html_node/Variadic-Functions.html

below link explain variadic macros very well:

https://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros

As per C99 6.7.5.3 Function declarators (including prototypes):

9. If the list terminates with an ellipsis (, ...), no information about the number or types

of the parameters after the comma is supplied.

C99 requires that

As per C99 6.10.3 Macro replacement:

10 A preprocessing directive of the form

# define identifier lparen identifier-listopt ) replacement-list new-line

# define identifier lparen ... ) replacement-list new-line

# define identifier lparen identifier-list , ... ) replacement-list new-line

defines a function-like macro with parameters, whose use is similar syntactically to a

function call.

An identifier __VA_ARGS_ _ that occurs in the replacement list shall be treated as if it

were a parameter, and the variable arguments shall form the preprocessing tokens used to

replace it.

EXAMPLE 7 Finally, to show the variable argument list macro facilities:

#define debug(...) fprintf(stderr, _ _VA_ARGS_ _)

#define showlist(...) puts(#_ _VA_ARGS_ _)

#define report(test, ...) ((test)?puts(#test):\

printf(_ _VA_ARGS_ _))

debug("Flag");

debug("X = %d\n", x);

showlist(The first, second, and third items.);

report(x>y, "x is %d but y is %d", x, y);

§6.10.3.5 Language 157

ISO/IEC 9899:TC3 Committee Draft — Septermber 7, 2007 WG14/N1256

results in

fprintf(stderr, "Flag" );

fprintf(stderr, "X = %d\n", x );

puts( "The first, second, and third items." );

((x>y)?puts("x>y"):

printf("x is %d but y is %d", x, y));