-
A tricky macro
Ok itz atleast tricky to me!
I want to write a macro which will do something
like
#define PRINTF(x) printf x
:
somewhere in code
:
PRINTF(("This is a zero %d\n", 0));
:
:
Works fine until now.
Now I need to make it something like
#define PRINTFX(x) \
fnCall(someparam, x)
Cant get this to work! Please note that x denotes variable number of parameters like printf.
The intended use is like
PRINTFX("One", "This is one %d\n", 1);
PRINTFX("Two", "Nothing);
Can add any number of paranthesis to the parameters.
Thanks for any help, nope this is not a brain teaser, it would be cool if I could make this work ... else the good old way, multiple functions with variable number of parameters to a function.
-
i don't think it's possible with #defines. printf is a weird function, though. it doesn't use multiple prototypes.
http://www.purdue.edu/PUCC/Short-Cou...s/p_00930.html
this should help answer questions
Edit: i read your post again and realized you knew this already. i don't think it's possible with #define. can't overload a define, nor can you just put it all in parenthesis without causing problems.
-
This might work for you, but you still have to use the (()) trick for all the additional parameters
Code:
#include <stdio.h>
#include <stdarg.h>
#define PRINTFX(x,y) \
fnCall( x, magic y )
void fnCall ( char *x, char *y ) {
printf( "%s %s\n", x, y );
}
char *magic ( const char *fmt, ... ) {
static char buff[BUFSIZ]; // must be static
va_list ap;
va_start ( ap, fmt );
vsprintf ( buff, fmt, ap );
va_end ( ap );
return buff;
}
int main ( ) {
PRINTFX( "One", ("This is one %d\n",1) );
PRINTFX( "Two", ("Nothing") );
return 0;
}
-
Thanks! That worked. Sorry I was tardy in getting back to the list.