Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass arguments to printf? 2

Status
Not open for further replies.

tjroamer

Programmer
Mar 25, 2005
8
0
0
DE
Hi,

I would like to write a function which wraps the printf(const char*, ...), in order to turn off it accroding to a macro definition, since I have a lot of debug information to be printed out, but it affects the performance of the program if I don't need debug any more.
The following function is what I wrote, but it does not work for other format output than the single string.
Code:
int pn_report(const char *format, ...) {
#ifdef PN_DEBUG
	return printf(format);
#else
	return 0;
#endif
}

if I call pn_report("ok"), it is fine, but if I call pn_report("%d", var), it is not ok.

any help on writing this function? Is there any other way to do this?

Thanks in advance.

/Kenny
 
I think C99 supports ellipsis in macros, I'm not too familiar with C99, but it definitely wouldn't work in C++.

Alternatively, you should be able to #define away the entire function call like this:
Code:
#ifdef PN_DEBUG
#  define pn_report( args )   printf( args )
#else
#  define pn_report( args )
#endif
But then I think you need to wrap your parameters in an extra set of parentheses when you call the function, ex:
Code:
pn_report( ("%d", var) );
 
Hi xwb,
these functions are exactly I am looking for. It helps me to wrap the error message I want to control. Thanks for your post.

Hi cpjust,
yes, you are right. I was very suprised that I got the compile error when I did not put a parenthesis like this:
Code:
pn_report(("%d", var));
thanks for your tip.
//Kenny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top