Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!sharkey!cfctech!teemc!ka3ovk!jando!spiff!spiff!gar From: gar@spiff.UUCP (Gordon Runkle) Newsgroups: comp.lang.c Subject: Re: #define DEBUG... (using printf for debugging) Message-ID: Date: 4 May 90 23:51:12 GMT References: <11290@hoptoad.uucp> <40628@cornell.UUCP> Sender: gar@spiff.uucp (Gordon Runkle) Followup-To: comp.lang.c Organization: MIC, Arlington, VA Lines: 67 In-Reply-To: gordon@mimir.cs.cornell.edu's message of 3 May 90 14:50:35 GMT In article <40628@cornell.UUCP> gordon@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=- -- UUCP: uunet!men2a!spiff!gar ARPA: men2a!spiff!gar@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