Thread: Help with #ifdef DEBUG #endif

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Help with #ifdef DEBUG #endif

    Hi All,
    I am very new to C (but have programmed in other languages) and have been asked to look at some program to modify. I have downloaded codeblocks-10.05mingw-setup.exe (windows). I am trying to break the program up so that I can understand it better.

    There are some sections in the code like

    #ifdef DEBUG
    fprintf(stderr, "Entering module\n");
    #endif

    How do I access the DEBUG block to see what it does through the CodeBlocks compiler? I tried DEBUG mode but it just runs and I do not see anything. Where is the stderr supposed to show? on the console?

    Thanks
    Lefti

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    DEBUG is not a block of anything... it's a defined symbol...

    If you #define DEBUG ... the code between #if and #endif is executed.
    If you don't it isn't.

    That's all there is to it.

    The C Preprocessor

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This has nothing to do with "debug mode". In your code, DEBUG is a macro specified by whoever wrote your code. The preprocessor does text substitution in a pre-compilation phase.

    In CodeBlocks, go to Project->Build Options....

    A window will pop up with several tabs. One of those is labelled "Compiler Settings". Select that. Within that, there will be another tab labelled #defines. Select that.

    That will show you a text edit pane. In that, define each macro you want to define.

    In your case (since using #ifdef DEBUG) enter "DEBUG" (without the quotes, case sensitive) on a line of its own. This is equivalent, at the top of your code, adding a line of the form "#define DEBUG".

    If you want the preprocessor to expand DEBUG to a value (for example, code has #if DEBUG == 2) then enter "DEBUG=2". This is equivalent, at the top of your code, to adding a line of the form "#define DEBUG 2".

    It would help if you read up on the C or C++ preprocessing. Any basic text on either language will describe enough for you to make sense of the above.
    Last edited by grumpy; 01-09-2012 at 06:25 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by CommonTater View Post
    DEBUG is not a block of anything... it's a defined symbol...

    If you #define DEBUG ... the code between #if and #endif is executed.
    If you don't it isn't.

    That's all there is to it.

    The C Preprocessor
    #if ( #if #ifndef #ifdef ) are compile time directives not run time directives so the code between #if and #endif is compiled if DEBUG is defined, and will execute like any other code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifdef DEBUG
    By lehe in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 03:26 PM
  2. I don't see the use of ifdef/endif in the following code
    By Overworked_PhD in forum C Programming
    Replies: 4
    Last Post: 01-18-2009, 01:46 PM
  3. #endif *
    By dwks in forum C Programming
    Replies: 9
    Last Post: 10-13-2005, 06:53 AM
  4. #endif ?
    By Zahl in forum C++ Programming
    Replies: 7
    Last Post: 11-12-2002, 12:20 PM
  5. #ifdef #endif
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2002, 08:28 PM