473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using ## with variadic macro?

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
Nov 14 '05 #1
1 1512
Michael B Allen wrote:
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.

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trent Buck | last post by:
(Note: C99 supports variadic macros, but C89 does not.) I'm pretty sure what I'm trying to do is impossible, but I'll ask here in case I'm missing something. I'm trying to define generic,...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
7
by: Michael B Allen | last post by:
If I define a variadic macro like say: #define PRINT(fmt, ...) _myprintf(__FILE__ ": " fmt, __VA_ARGS__) and I call this like: PRINT("no args"); the preprocessor generates:
3
by: Thomas Carter | last post by:
I understand that C99 supports variadic macros. However, is it not the case that a variadic macro defined as #define SAMPLE_MACRO(...) Bloody-blah must take at least one argument? I would be...
15
by: Urs Thuermann | last post by:
I want to write a macro that produces debug output and has a variable number of arguments, so that I can use like this: int i; char *s; DBG("simple message\n"); DBG("message with an int...
14
by: jontwang | last post by:
How do you get a count of the number of arguments passed into a variadic macro?
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
8
by: keith | last post by:
Someone please help me out here - I'm having a 'bad brain' day. I have a number of C logging functions, prototyped as, say: extern "C" { mylog1(char *arg1, char *arg2, char *arg3);...
2
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. ...
8
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.