Thread: Programming style: #ifdef vs regular if

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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, 02: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, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM