Reputation: 1324
recently I was browsing SO and I came across this topic, where Sam Jansen is declaring a macro PACKED_STRUCT(name)
, but in code he is using it once with no arguments and once with the name argument supplied.
I tried to compile a similar sample progam with GCC 4.6.1 and I was surprised it compiled fine with no warnings at all (I was using -std=c99 -Wall -Wextra -pedantic
command line switches).
However when I tried to make two argument macro and call it with less than two arguments, it did not work.
So my question is if this is a bug in GCC, or it is a feature of GCC, or it is defined somewhere in the standard, that it has to work like this?
According to this page in GCC's documentation, this should not be possible.
Upvotes: 3
Views: 200
Reputation: 140569
Read that page of GCC's documentation again; it's making a distinction between empty arguments and missing arguments. Given
#define ONE(x) one(x)
#define TWO(x,y) two(x,y)
all of these are perfectly fine as far as the preprocessor is concerned (expansion in comment):
ONE(1) /* one(1) */
ONE() /* one() */
TWO(1,2) /* two(1,2) */
TWO(1,) /* two(1,) */
TWO(,2) /* two(,2) */
TWO(,) /* two(,) */
but this is not okay:
TWO() /* error: macro "TWO" requires 2 arguments, but only 1 given */
Upvotes: 3