How do you define a macro with an unspecified number of arguments? For example, if you want to define a macro called DBG with at least one argument, that calls printf with all arguments and then prints out a new line at the end, how can you do that?
How do you define a macro with an unspecified number of arguments? For example, if you want to define a macro called DBG with at least one argument, that calls printf with all arguments and then prints out a new line at the end, how can you do that?
By using a function instead and using vprintf
What!? vprintf? That wasn't what I said! Ok, I want to write a macro, you can take a look again at the top statement.Originally Posted by Thantos
I know what you said. Just because you want something doesn't mean its a good idea.
Or even that it can be done. C isn't about what you want. Unless of course the language happens to agree with you.
Quzah.
Hope is the first step on the road to disappointment.
I know it can be done! I have seen just such a macro, but I didn't quite understand the code, I think it was using some defined thing I didn't know what it was then, when it called printf, it used the defined thing instead of the "optional" arguments. And the macro definition was "#define DBG(x, ...) " and then something. Isn't there anyone who knows how to do?Originally Posted by quzah
http://c-faq.com/cpp/varargs.html
[edit]It looks like someone followed the link!gcc has an extension which allows a function-like macro to accept a variable number of arguments, but it's not standard.
Last edited by Dave_Sinkula; 07-21-2006 at 08:11 PM. Reason: ...
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
If you are using GCC, you can have variadic macros as an extension. If this is applicable to you, check the docs for more details.
System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.
Useful resources:
comp.lang.c FAQ | C++ FQA Lite
I've seen fflush( stdin ) and void main too. What was your point again?Originally Posted by TriKri
Quzah.
Hope is the first step on the road to disappointment.
They're new in C99
http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
While it's true variable argument lists of macros are now supported in C99, the link is wrong on one count. Very few C++ compilers provide this feature (as an extension).Originally Posted by Salem
In C++ and C89, if you want something with variable argument lists, you need to use variable argument functions.
If the usage you intend is essentially a wrapper for a variable argument function, it is possible to do a cludge in both C89 and C++. For example;
There are catches with this. The first is that it is "all or nothing" --- to illustrate, in this example, it is not possible to change the PRINT macro so it specifies the first argument to fprintf() but allowing the remaining lis of arguments provided to printf() to be variable. The need for using two sets of brackets, at the point where the macro is used, is a gotcha for whoever uses the macro --- if they forget the two sets, the result is typically a lot of confusing error messages from the compiler.Code:#define PRINT(x) fprintf(x) int main() { PRINT((stdout, "Hello %s\n", "world")); /* note use of two sets of () on macro use */ }
Do you say it's a bad idea or what!?Originally Posted by quzah
Are you trolling? Those answers are in the FAQ.Originally Posted by TriKri
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Trolling what? Isn't that the same as fishing? FAQ's is allways so hard to search when looking for such a specific thing as "macros with an unspecified number of arguments".Originally Posted by Dave_Sinkula
Just another question, why shouldn't I use fflush(stdin) or what should I use instead? And I'm sorry quzah if I have been rude to you, I didn't mean to be.Originally Posted by quzah