#define DEBUG... (using printf for debugging)
Gordon Runkle
gar at spiff.UUCP
Sat May 5 09:51:12 AEST 1990
In article <40628 at cornell.UUCP> gordon at mimir.cs.cornell.edu (Jeffrey Adam Gordon) writes:
I want to have a DEBUG flag which controls whether diagnostic printfs
are executed or not.
But its a pain to have to type the #ifdef ... #endif all the time, and
its less readable than simply having:
DEBUG ("debugging information");
Now, I can use the latter format if I
#define DEBUG printf
but then how do I turn DEBUG off?
I have though of doing the following (which is not very elegant but I
thought it would work):
[ valiant attempt...]
There is a simple solution. Get the book "Debugging C" by Robert Ward
(Que, Indianapolis, Indiana, ISBN: 0-88022-261-1).
On page 106 is an example of exactly your problem. I will explian it,
but I strongly recommend you buy the book. It's very useful.
#ifdef TESTING
# define TRACE(args) fprintf args
#else
# define TRACE(args)
#endif
This macro means that TRACE(args) will have a value if TESTING is
defined. Otherwise, TRACE(args) will have no value, and the
preprocesor will eliminate it. You want args to be affected too,
for obvious reasons.
When you use it, use *2 sets* of parentheses around the args, to
insure that the entire list of args is given to fprintf():
main(argc, argv)
int argc;
char *argv[];
{
TRACE((stderr, "argc is %d\n", argc));
if (argc > 1)
printf ("hello world\n");
}
In this example, if TESTING is defined, the TRACE statement will be
converted to:
fprintf (stderr, "argc is %d\n", argc);
otherwise, it will be reduced to ";", an empty statement.
It's pretty simple, really. Again, I really recommend the book.
-=gordon=- <Gordon Runkle>
--
UUCP: uunet!men2a!spiff!gar ARPA: men2a!spiff!gar at uunet.uu.net
Management Information Consulting, Inc. HOME PH: 703-522-0825
2602 Lee Hwy, # 204, Arlington, VA 22201 WORK PH: 202-566-4382
"Just once I'd like to run across an alien menace that
isn't immune to bullets." -- The Brigadier
More information about the Comp.lang.c
mailing list