PDA

View Full Version : how to define a macro


dvmoinescu
08-10-2005, 02:56 PM
... with variable arguments.

I would like to define a macro like

#ifdef _DEBUG
# define _DEBUG_TRACE(...args...) {\
fprintf(stderr, ...args...);\
fflush(stderr);\
}
#else
# define _DEBUG_TRACE(...args...)
#endif


This MACRO should receive a variable number of arguments that are passed to fprintf() function.

some examples:
_DEBUG_TRACE("It works\n");
_DEBUG_TRACE("It %s\n", "works");
_DEBUG_TRACE("It %s%s", "works", "\n");
_DEBUG_TRACE("%s %s%c", "It", "works", '\n');

i3839
08-10-2005, 04:21 PM
#include <stdio.h>

#define p(arg...) printf(arg);

int main(){
p("1\n");
p("%s\n", "2");
p("%s%s", "3", "\n");
return 0;
}

Some notes though: stderr isn't buffered, so flushing it does nothing. Also stdout which is buffered is automatically flushed after each '\n', so flushing that makes no sense either most of the time.

RobSeace
08-10-2005, 06:05 PM
Also, if you want some fixed args before the varargs in the macro, and you want to
allow for the varargs to possibly not be specified at all, you have to jump through a
few more hoops... Eg: tweaking i3839's example:


#define P(fmt,args...) printf (fmt , ## args)


That may be GCC-specific trick... Basically, in the case of "args" being empty, it
eats the "," and makes it go away, but if there actually are some args, it keeps it...

In fact, I think the whole "args..." style syntax is GCC-specific, and the C99 way to
do it is to use just "..." and then in the expansion to reference "__VA_ARGS__" in
place of "args"... Personally, I think the GCC way is much nicer...

i3839
08-10-2005, 10:23 PM
#include <stdio.h>

#define p(arg...) printf(arg);
#define x(fmt,args...) printf (fmt , ## args)

int main(){
p("1\n");
p("%s\n", "2");
p("%s%s", "3", "\n");
x("4\n");
x("%s\n", "5");
x("%s%s", "6", "\n");
return 0;
}

This works when compiling with -std=c99, so I assume it's not a GNU extension.

RobSeace
08-11-2005, 12:26 PM
I know I read somewhere something about that being a GCC-ism... Hmmm... Oh
yeah, in the stupid "info" pages... (Man, do I hate those lousy things... Man pages
with delusions of being web pages... ;-/ All with a counter-intuitive Emacs-inspired
UI... Lovely... ;-/) Do "info gcc", then hit enter on "C Extensions", then on "Variadic
Macros":


Macros with a Variable Number of Arguments.
===========================================

In the ISO C standard of 1999, a macro can be declared to accept a
variable number of arguments much as a function can. The syntax for
defining the macro is similar to that of a function. Here is an
example:

#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)

Here `...' is a "variable argument". In the invocation of such a
macro, it represents the zero or more tokens until the closing
parenthesis that ends the invocation, including any commas. This set of
tokens replaces the identifier `__VA_ARGS__' in the macro body wherever
it appears. See the CPP manual for more information.

GCC has long supported variadic macros, and used a different syntax
that allowed you to give a name to the variable arguments just like any
other argument. Here is an example:

#define debug(format, args...) fprintf (stderr, format, args)

This is in all ways equivalent to the ISO C example above, but
arguably more readable and descriptive.

GNU CPP has two further variadic macro extensions, and permits them
to be used with either of the above forms of macro definition.

In standard C, you are not allowed to leave the variable argument out
entirely; but you are allowed to pass an empty argument. For example,
this invocation is invalid in ISO C, because there is no comma after
the string:

debug ("A message")

GNU CPP permits you to completely omit the variable arguments in this
way. In the above examples, the compiler would complain, though since
the expansion of the macro still has the extra comma after the format
string.

To help solve this problem, CPP behaves specially for variable
arguments used with the token paste operator, `##'. If instead you
write

#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)

and if the variable arguments are omitted or empty, the `##'
operator causes the preprocessor to remove the comma before it. If you
do provide some variable arguments in your macro invocation, GNU CPP
does not complain about the paste operation and instead places the
variable arguments after the comma. Just like any other pasted macro
argument, these arguments are not macro expanded.


So, according to that, at least, it's GCC-ism... But, whether or not the docs are
lying (which is ALWAYS a possibility), I have no idea... *shrug* In any event, I
use it, and like it, and don't really care if it's a GCC-ism, personally... ;-)

i3839
08-11-2005, 01:46 PM
$ gcc -pedantic varmacro.c
varmacro.c:3:14: warning: ISO C does not permit named variadic macros
varmacro.c:4:19: warning: ISO C does not permit named variadic macros
varmacro.c:10:9: warning: ISO C99 requires rest arguments to be used
Ah, there you go. Seems like you aren't the only one who likes it and doesn't care it's non-standard. ;-)