Thread: What exactly does #ifdef DEBUG do?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you decide to define this variable then whatever is enclosed in the #ifdef / #endif blocks will be compiled. Otherwise not. You can define it by including #define DEBUG at the top (in the source code), or as a flag in the compile parameters I believe. So you don't have to physically alter the source file.

    Presumably these #ifdef / #endif blocks contain diagnostic code that's used to output descriptive messages to help trace and verify the program's execution.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    40
    Quote Originally Posted by nonoob View Post
    If you decide to define this variable then whatever is enclosed in the #ifdef / #endif blocks will be compiled. Otherwise not. You can define it by including #define DEBUG at the top (in the source code), or as a flag in the compile parameters I believe. So you don't have to physically alter the source file.

    Presumably these #ifdef / #endif blocks contain diagnostic code that's used to output descriptive messages to help trace and verify the program's execution.
    Ok this is probably a stupid question. But, if I don't want to compile a part of the program, while can't I just take that part out and keep it in another file?

    Does Debug actually tell you where the problem occurs?

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by pyroknife View Post
    Ok this is probably a stupid question. But, if I don't want to compile a part of the program, while can't I just take that part out and keep it in another file?

    Does Debug actually tell you where the problem occurs?
    There would be a chance that you put it back in your code incorrectly if you are copying and pasting - Especially if you are modifying code as you go.

    There is something that does help you debug -> It's called "assert" and it works in a similar way.

    Basically, you include assert.h - When you are making your program, you put asserts in to make sure all your assumptions are correct. (For example, in another thread (I can't remember which one) I have suggested that the OP asserts that a variable is never 0)

    When the algorithm is working and you know it won't be 0, you define NDEBUG at the top of your code. All asserts are then optimised out of existence.

    I'll give you an example if you wanted to get it working - Basically I've put in a few errors for you to find using the asserts. This little mess of a program was supposed to find the element in the array before '9' and print it out.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    
    int get_preceding(int arr[], size_t arr_size, int element);
    
    
    int main(void)
    {
        int my_arr[10] = {3, 6, 1, 4, 5, 2, 7, 8, 9, 10};
        int i;
    
    
        for (i=0; i<15; i++)
        {
    
    
            int preceding_element;
            preceding_element = get_preceding(my_arr, sizeof(my_arr)/sizeof(*my_arr), i);
    
    
            if (my_arr[i]==9)
            {
                printf("%d", preceding_element);
            }
        }
    
    
    
    
        return EXIT_SUCCESS;
    }
    
    
    int get_preceding(int arr[], size_t arr_size, int element)
    {
    
        /* These asserts will automatically go when NDEBUG is defined */
    
        assert(arr != NULL);
        assert((element - 1) >= 0);
        assert(element < arr_size);
    
    
        /* This section has been manually designated to go 
            when NDEBUG is defined via the line below (finishes at #endif) */
    
    #ifndef NDEBUG
    
    
        // Once the program is running, I won't care about this...
        printf("(%d) ", arr[element - 1]);
    
    
    #endif
    
    
        return arr[element - 1];
    }
    When you are confident that the program is working, you can define NDEBUG and all the asserts are optimised out of your code.

    I like to make all my debug stuff around the define of NDEBUG -> That way it fits in nicely with the assert library.

    [edit]
    I forgot to say
    #ifndef -> "if not defined"
    [/edit]

    [edit]
    I reread the post and decided that it needed a few comments
    [/edit]
    Last edited by Click_here; 01-21-2013 at 08:38 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with #ifdef DEBUG #endif
    By lefti in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:26 PM
  2. #ifdef DEBUG
    By lehe in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 03:26 PM
  3. #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
  4. #ifdef and so on.
    By MipZhaP in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 05:34 PM
  5. How To use #ifdef ?
    By GaPe in forum Windows Programming
    Replies: 1
    Last Post: 11-01-2003, 12:57 PM