Programming style: #ifdef vs regular if

This is a discussion on Programming style: #ifdef vs regular if within the C Programming forums, part of the General Programming Boards category; Except for the often-frowned-upon way of including #ifdef's in a .c file, there are two ways to include debug code ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Programming style: #ifdef vs regular if

    Except for the often-frowned-upon way of including #ifdef's in a .c file, there are two ways to include debug code in a program:
    1. In xxx.h:
    Code:
    #ifdef DEBUG
      void debugprint(const char *s);
    #else
      static inline debugprint(const char *dummy) {}
    #endif
    And in xxx.c:
    Code:
    int somefunction(...)
    {
       ...
       debugprint("...debug info...");
       ...
    }
    2. In xxx.c:
    Code:
    int somefunction(...)
    {
       ...
       if (DEBUG) 
         debugprint("...debug info...");
       ...
    }
    The second method can test all possible execution paths of a program during compile time but the code is littered with "if (DEBUG)". DEBUG is defined in a command-line compiler option (-D). What one is better? Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you don't want to do as in the 2d example because that is executable code in release-mode programs too. In the 1st example, function debugprint() only appears in the program when DEBUG is defined. If it is not defined then a good optimizing compiler will toss out the function because it does nothing.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    you don't want to do as in the 2d example because that is executable code in release-mode programs too.
    hmm... but wouldnt a "good optimizing compiler" also ignore the debugprint() call in the 2nd example if DEBUG is 0?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    In the first example, any non-retarded, non-optimizing compiler will remove the call because that's what the preprocessing language tells it to do.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by laserlight
    hmm... but wouldnt a "good optimizing compiler" also ignore the debugprint() call in the 2nd example if DEBUG is 0?
    No. I was incorrect in my oritinal post. DEBUG is defined only for debug builds, and the compiler will generate an error on release builds because DEBUG is undefined.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    DEBUG is defined only for debug builds, and the compiler will generate an error on release builds because DEBUG is undefined.
    I think that's only the first example, since the OP did say "DEBUG is defined in a command-line compiler option (-D)" for the second example, though he/she was rather vague about it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Clarification

    I guess my second method needs to set DEBUG to a logical value, not just define or not define it. So, for the second method, the compilation command would be:
    cc -DDEBUG=true myprog.c
    or
    cc -DDEBUG=false myprog.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  2. #ifdef - Can It Be Used With Logic Such as OR / AND?
    By dedham_ma_man in forum C Programming
    Replies: 3
    Last Post: 04-21-2006, 03:57 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 12:21 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21