I'd like to use a macro like the following:
#define x(...) y(a,##__VA_ARGS__,b)
To expand like so:
x(); -> y(a,b);
x(1); -> y(a,1,b);
With -std=gnu99
, it works perfectly.
With -std=c99
however, it looks like this:
x(); -> y(a,,b);
x(1); -> y(a,1,b);
The ##
is making no difference – it's not swallowing the comma.
In other usages under C99, e.g. #define x(a,...) y(a,##__VA_ARGS__)
, comma-swallowing works fine.
What can I do, if anything, to get the desired comma-swallowing behaviour under clang's -std=c99
, either with the GNU extension ##
or by some other method?
As you describe, the following pattern works fines.
After I run the following code snip, I guess
##__VA_ARGS__
will always swallow comma when the prefix args exists(such as _ in the e_x)You can get the result:
Above sample works fine in my computer(both macos and linux) .
Yes,
##__VA_ARGS__
is a GNU extension.That's expected behaviour. There is no standard way to swallow the comma.
Fortunately,
gcc
,clang
andxlc
support the##__VA_ARGS__
gnu extension, andmsvc
swallows the comma automatically.If you don't want to rely on above mentioned language extensions, the idiomatic ISO C90 way to get variable argument macros was like this:
If you don't want to use either of these solutions, you can always employ argument counting, as answered here.