Xref: utzoo comp.lang.c:28430 comp.unix.wizards:21795 alt.sources:1835 comp.sources.d:5262 misc.misc:9665 Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!unisoft!peritek!dig From: dig@peritek.UUCP (Dave Gotwisner) Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc Subject: Re: #define DEBUG... (using printf for debugging) Message-ID: <1427@peritek.UUCP> Date: 4 May 90 04:47:41 GMT References: <11290@hoptoad.uucp> <40628@cornell.UUCP> Organization: Peritek Corp., Oakland, CA Lines: 54 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. > > The obvious way to do this is: > > #ifdef DEBUG > printf ("debugging information"); > #endif DEBUG > > But its a pain to have to type the #ifdef ... #endif all the time, and > its less readable than simply having: > > DEBUG ("debugging information"); Agreed. > > Now, I can use the latter format if I > > #define DEBUG printf > > but then how do I turn DEBUG off? Having read some of the other responses, I have yet to see the one I use: #ifdef DEBUG # define Dprintf printf #else # define Dprintf if #endif This only needs to be included in a common header file, then, if you do: Dprintf("Input value is %d\n", 10); you get the following if DEBUG is defined: printf("Input value is %d\n", 10); and the following if DEBUG is not defined: if ("Input value is %d\n", 10); The semicolon will terminate the if, so, unless the debug printf has an else immediatly after it, this will work. There is some run time overhead (unless your compiler optimizes "if (statement);" away because it never does anything), but the overhead should be small. -- ------------------------------------------------------------------------------ Dave Gotwisner UUCP: ...!unisoft!peritek!dig Peritek Corporation ...!vsi1!peritek!dig 5550 Redwood Road Oakland, CA 94619 Phone: 1-415-531-6500