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?
Printable View
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.Quote:
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.
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?Quote:
Originally Posted by quzah
http://c-faq.com/cpp/varargs.html
[edit]It looks like someone followed the link!:pQuote:
gcc has an extension which allows a function-like macro to accept a variable number of arguments, but it's not standard.
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.
I've seen fflush( stdin ) and void main too. What was your point again?Quote:
Originally Posted by TriKri
Quzah.
They're new in C99
http://david.tribble.com/text/cdiffs.htm#C99-pp-vararg
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).Quote:
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!?Quote:
Originally Posted by quzah
Are you trolling? Those answers are in the FAQ.Quote:
Originally Posted by TriKri
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".Quote:
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.Quote:
Originally Posted by quzah
TriKi: Trolling is bad.
Like here:Quote:
Originally Posted by Dave_Sinkula
FAQ > Explanations of... > Why fflush(stdin) is wrong
FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
Quote:
Originally Posted by quzah
I am honestly sorry if people sees me as a troller, that has not been my purpose, I really wan't to know the language better. Maybe not the best things to ask for if I take a look at it now, specially judging by the replies I have got... I guess I have to ask the original code writers. ;)
I don't really see how this matters. In real applications cross compatability is rarely an issue. I switched to linux from bsd because the same people that constantly complain not enough applications that run on windows (flash 9 for example) run on linux write applications that don't work correctly on bsd, which even provides a linux ABI compatability layer.Quote:
In C++ and C89, if you want something with variable argument lists, you need to use variable argument functions.
There are plenty of things C99 added that aren't compatible with C++, this is because C isn't a subset of C++, they have become separate languages.
Why would anyone want to compile a C application with a C++ compiler anyway?
The designer of an application, rather than meticulously checking everything works correctly in all environments should analyze who the target audience is (how their application would factor into each environment) and who they want to support.
For example, it's pretty damn hard to find console applictions for windows simply because windows is built to have graphical stuff (the display runs at ring 0 after all)--thus the designer shouldn't really worry about using unix/posix specific calls.
Another example would be someone designing an appliction for kde that doesn't run on openbsd, not a big deal because if you're running openbsd with kde you're diseased and no one cares about lepers.
I now end my fat post.