Using ## with variadic macro?

M

Michael B Allen

Is the following legit?

#define PRINT(err, fmt, ...) \
my_printf(__FILE__ ": %s" fmt, errstr(err), ##__VA_ARGS__)

Without the ## if I call this like PRINT(EFOO, ": foo!") this reduces
to following invalid function call:

my_printf("file.c" ": %s" ": foo!", errstr(err),)

But with gcc at least the ## ensures the trailing comma is removed if
no rest arguments are provided. Is this gcc specific is it valid C99?

Thanks,
Mike
 
I

Ian Pilcher

Michael said:
Is the following legit?

#define PRINT(err, fmt, ...) \
my_printf(__FILE__ ": %s" fmt, errstr(err), ##__VA_ARGS__)

Without the ## if I call this like PRINT(EFOO, ": foo!") this reduces
to following invalid function call:

my_printf("file.c" ": %s" ": foo!", errstr(err),)

But with gcc at least the ## ensures the trailing comma is removed if
no rest arguments are provided. Is this gcc specific is it valid C99?

I've been wrestling with a similar problem. If you compile this code
with -std=c99 -Wall -W -pedantic, I believe that you'll get a warning
about unused rest arguments. Based on this, I believe that this is a
GCC extension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top