-
November 10th, 2006, 03:43 PM
#1
Alternate printf() function
Hi all,
I am faced with a tricky issue and would very much like somebody to either give me a solution or some useful pointers.
I would like to add a feature in my program that will 'bypass' the printf() calls if a particular condition is not met. Now I may have a lot of printf()s all over the program so checking for the condition near each such function is not a feasible option.
My code should look something like this
Code:
PRINT(*parameter*)
{
#ifndef CONDITION
printf(*parameter*)
#endif
}
void main()
{
PRINT(*anything can go in here*);
}
I am open to any other ideas.
Thanks,
Leo
-
November 10th, 2006, 05:05 PM
#2
Re: Alternate printf() function
Yes, it's possible.
You can write a variadic function, using <stdarg.h> in a C program or <cstdarg> in a C++ program.
Code:
#include <stdarg.h>
int print(const char* Format, ...) {
if (condition) {
va_list ls;
int r;
va_start(ls, Format);
r=vprintf(Format, ls);
va_end(ls);
return r;
}
return 0;
}
Have a look at:
http://www.cppreference.com/stdio/vp..._vsprintf.html
PS: "void main()" is not a correct prototype for the main function. You should use "int main()".
Last edited by SuperKoko; November 10th, 2006 at 05:12 PM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
November 10th, 2006, 07:34 PM
#3
Re: Alternate printf() function
Thanks SuperKoko...it works just as expected!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|