Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

vicviper

defining macros with a variable number of parameters

This topic is 5267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi! I usually define macros this way: #define mymacro(x,y) myfunction(x,y) and this works smooth... but, how can i do this: #define myprintf( ??? ) printf( ??? ) en then, use the myprintf macro with a different number of parameters ? like this: myprintf("hello world"); myprintf("%d %d",x,y); is this possible? is there any other alternative to do this?

Share this post


Link to post
Share on other sites
Advertisement
Whether it is possible or not depends entirely on your compiler, as it would be a language extension. Check your documentation.

For custom printf functions, check out vprintf.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
C99 introduced variable argument function macros. With those, you can put an ellipses in the macro declaration to denote the use of variable number of arguments, and access the arguments with __VA_ARGS__. One example of a such a macro would be:

#define DEBUGF(f,...) \
(fprintf(dbgf, "%s(): ", f), fprintf(dbgf, __VA_ARGS__))


Of course if you''re not working with C99, then you would need to hope for a vendor specific preprocessor extension.

Share this post


Link to post
Share on other sites
It is not possible I''m afraid, the only way would be to use a function call with the ... in the parameters

Share this post


Link to post
Share on other sites
quote:
Original post by Trip99
It is not possible I''m afraid, the only way would be to use a function call with the ... in the parameters

...

Yes I should say my answer applies to Visual Studio only.



Variadic functions are standard C, not VS-specific.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
if you use
void foo(...)
{ }
is there some way to acess the arguments one by one or do you have to deal with the whole chunk?

Share this post


Link to post
Share on other sites
It can be done, sort of:

#define MY_PRINTF(args) printf args

and to use it:

MY_PRINTF (("format_string", arg1, arg2));

Note the double parenthesis and the lack of parenthese around the ''args'' in the macro expansion. It''s a bit of a hack and the compiler will barf when you forget to put the ''(('' or ''))'' in.

If you''re using C++, you can use the << operator instead:

#define MY_PRINTF(args) printf_object << args

MY_PRINTF ("string" << arg1 << "string" << arg2);

which is a bit better.

Skizz

Share this post


Link to post
Share on other sites
#define myprintf printf

Just miss the brackets off the macro. This topic comes up all the time.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!