Go to the first, previous, next, last section, table of contents.


Macros with Variable Numbers of Arguments

In GNU C, a macro can accept a variable number of arguments, much as a function can. The syntax for defining the macro looks much like that used for a function. Here is an example:

#define eprintf(format, args...)  \
 fprintf (stderr, format , ## args)

Here args is a rest argument: it takes in zero or more arguments, as many as the call contains. All of them plus the commas between them form the value of args, which is substituted into the macro body where args is used. Thus, we have this expansion:

eprintf ("%s:%d: ", input_file_name, line_number)
==>
fprintf (stderr, "%s:%d: " , input_file_name, line_number)

Note that the comma after the string constant comes from the definition of eprintf, whereas the last comma comes from the value of args.

The reason for using `##' is to handle the case when args matches no arguments at all. In this case, args has an empty value. In this case, the second comma in the definition becomes an embarrassment: if it got through to the expansion of the macro, we would get something like this:

fprintf (stderr, "success!\n" , )

which is invalid C syntax. `##' gets rid of the comma, so we get the following instead:

fprintf (stderr, "success!\n")

This is a special feature of the GNU C preprocessor: `##' before a rest argument that is empty discards the preceding sequence of non-whitespace characters from the macro definition. (If another macro argument precedes, none of it is discarded.)

It might be better to discard the last preprocessor token instead of the last preceding sequence of non-whitespace characters; in fact, we may someday change this feature to do so. We advise you to write the macro definition so that the preceding sequence of non-whitespace characters is just a single token, so that the meaning will not change if we change the definition of this feature.


Go to the first, previous, next, last section, table of contents.