home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1265 < prev    next >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: dig@peritek.UUCP (Dave Gotwisner)
  2. Newsgroups: comp.lang.c,comp.unix.wizards,alt.sources,comp.sources.d,misc.misc
  3. Subject: Re: #define DEBUG... (using printf for debugging)
  4. Message-ID: <1427@peritek.UUCP>
  5. Date: 4 May 90 04:47:41 GMT
  6.  
  7. In article <40628@cornell.UUCP>, gordon@mimir.cs.cornell.edu (Jeffrey  Adam Gordon) writes:
  8. > I want to have a DEBUG flag which controls whether diagnostic printfs
  9. > are executed or not.
  10. > The obvious way to do this is:
  11. > #ifdef DEBUG
  12. >     printf ("debugging information");
  13. > #endif DEBUG
  14. > But its a pain to have to type the #ifdef ... #endif all the time, and
  15. > its less readable than simply having:
  16. >
  17. >     DEBUG ("debugging information");
  18.  
  19. Agreed.
  20.  
  21. > Now, I can use the latter format if I
  22. > #define DEBUG printf
  23. > but then how do I turn DEBUG off?
  24.  
  25. Having read some of the other responses, I have yet to see the one I use:
  26.  
  27. #ifdef DEBUG
  28. # define    Dprintf    printf
  29. #else
  30. # define    Dprintf if
  31. #endif
  32.  
  33. This only needs to be included in a common header file,  then, if you do:
  34.  
  35.     Dprintf("Input value is %d\n", 10);
  36.  
  37. you get the following if DEBUG is defined:
  38.  
  39.     printf("Input value is %d\n", 10);
  40.  
  41. and the following if DEBUG is not defined:
  42.  
  43.     if ("Input value is %d\n", 10);
  44.  
  45. The semicolon will terminate the if, so, unless the debug printf has an
  46. else immediatly after it, this will work.  There is some run time overhead
  47. (unless your compiler optimizes "if (statement);" away because it never does
  48. anything), but the overhead should be small.
  49. -- 
  50. ------------------------------------------------------------------------------
  51. Dave Gotwisner                    UUCP:  ...!unisoft!peritek!dig
  52. Peritek Corporation                       ...!vsi1!peritek!dig
  53. 5550 Redwood Road
  54. Oakland, CA 94619                Phone: 1-415-531-6500
  55.