HP C A.06.05 Reference Manual

HP C/HP-UX Implementation Topics
The varargs Macros
Chapter 10 257
printf(f, s)
How HP C implements GNU and C9X macros
If you intend to use GNU style variable argument macros in HP C, note that you can make
the concatenation operator ## prevent syntax errors from occurring when the variable
argument comes in as empty (the null string). However, you can also insert whitespace to the
left of the left operand of ## to more accurately specify the intended left operand.
For example, if you use
#define foo(f, s...) printf(f, s)
Then the macro "call"
foo("Hello world.\n");
results in the expansion
printf("Hello world.\n",);
(note the comma ",") causing a syntax error.
GNU provides the following workaround for this kind of a situation. If you use:
#define foo(f, s...) printf(f, ## s)
If the variable parameter s is non-null, if for example, you use:
foo("%s %d\n", "Cycles", "1024");
the result is
printf("%s %d\n", "Cycles", "1024");
as the expansion as you would expect.
However, if s is null, this erases the comma to the left of the ## in the macro definition and
resulting expansion is:
printf("Hello world.\n");
Note that the comma is gone.
In order to get the same behavior in HP C, you must insert a space to the left of the comma to
make it clear to the preprocessor that the comma is the left operand of the '##' operator. Thus
your definition for the macro foo is:
#define foo(f, s...) printf(f , ## s)
(Note the space to the left of the ## operator in the macro definition.)
If the space is not inserted, the left operand of the ## operator is understood to be: