Thread: using #if debug 1

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    using #if debug 1

    i have some code that maybe able to be reused if i can add an extra for loop
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define debug 0
    int main()
    {
        int i, j, x, y =3;
        if (y == 3)
        {
            #undef debug
            #define debug 1
    #if debug 1
        }
        for (j = 0 j< 2; j++)
        {
     #endif // debug
            for (1 = 0; i < 5; i++)
            {
                x += 1;
            }
    #if debug 1
        }
    #endif // debug
        return 0;
    }
    i know this is badly written but the principle is if y is 3 add the extra for loop so x will be 10. if y isnt 3 then just use the inner loop ( x will be 5)

    is there away to do this that doesn't cause umpteen errors

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    Define the symbol at the command line:
    Code:
    $ gcc -DDEBUG -g -o test test.c
    Or NDEBUG, since functions as assert() use this symbol to disable debugging... So, instead of #ifdef you can use #ifndef.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    debug was the wrong use in that sense. i was trying to turn a for loop on and off so that i can reuse a function rather having the same code repeated with an extra for loop ie
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void func1(void);
    void func2(void);
    int main()
    {
        int y =3;
        if (y == 3)
        {
            func2();
        }
        else
        {
            func1();
        }
        return 0;
    }
    void func1(void)
    {
        int i, x;
        for (i=0;i<=5; i++)
        {
            x += 1;
        }
        printf("%d",x);
    }
    void func2(void)
    {
        int i, j, x;
        for (j=0;j<=2;j++)
        {
            for (i=0;i<=5; i++)
            {
                x += 1;
            }
        }
        printf("%d",x);
    }
    hope this is clearer
    coop

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,232
    I would think about how to structure the code such that you can call func1 from func2 to achieve reuse. Trying to turn on/off a loop in a function via a macro for non-debugging purposes is not a good idea.

    By the way, you forgot to initialise x.
    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

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    so i could do something like
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int func1(void);
    void func2(void);
    int main()
    {
        int x=0, y =3;
        if (y == 3)
        {
            func2();
        }
        else
        {
           x= func1();
           printf("%d",x);
        }
        return 0;
    }
    int func1(void)
    {
        int i, x=0;
        for (i=0;i<=5; i++)
        {
            x += 1;
        }
        return x;
    }
    void func2(void)
    {
        int j, x=0;
        for (j=0;j<=2;j++)
        {
            x += func1();
        }
        printf("%d",x);
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,232
    Yes, although you might want func2 to return instead of print too, then you only need one printf function (in main).

    By the way, I know it's only a toy example otherwise you would be using multiplication, but when you want to loop N times, consider writing your for loops in either this form:
    Code:
    for (i = 0; i < N; i++)
    or this form:
    Code:
    for (i = 1; i <= N; i++)
    Combining starting from 0 with <= might make your code more prone to off-by-one errors as it is not as typical.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug please
    By progmateur in forum C Programming
    Replies: 2
    Last Post: 03-23-2013, 01:47 PM
  2. Debug Help
    By strokebow in forum Game Programming
    Replies: 2
    Last Post: 11-17-2008, 07:44 PM
  3. can someone help me debug this?
    By steve568 in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 07:35 AM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  5. please help me debug
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 04-20-2002, 07:05 PM

Tags for this Thread