0

I am really new to programming (I'm an electronics and comm. engineer) and I am not able to figure out why one program works and the other one doesn't.

I'd like to get a good understanding of recursive functions before going any further with my learning. I would appreciate any help regarding this.

I know the difference between the x++ and --x. But in this context of this program, I thought both of these programs should run the same way. But they don't.

void rec(int x)
{
    if(x>0)
        rec(x--);
    printf("%d",x);
}

int main()
{
    rec(4);
    _getch();

} /*This doesn't work. And shows a stackoverflow */

void rec(int x)
{
    if(x>0)
        rec(--x);
    printf("%d",x);
}

int main()
{
    rec(4);
    _getch();

} /*This gives a proper output as expected*/ 
/*The output is 00123*/

Thanks!

P.S: Forgive me if this is a trivial or stupid question, but I am stuck on this and I thought this is the best place I can look for help.

Littm
  • 4,923
  • 4
  • 30
  • 38
umayneverknow
  • 190
  • 1
  • 3
  • 13
  • You really should enable warnings and debugging info at compilation time (e.g. compiling with `gcc -Wall -g` on Linux), and you should learn to use a debugger (e.g. `gdb` on Linux), notably step-by-step execution and displaying of data. – Basile Starynkevitch Oct 01 '12 at 05:26
  • I'm currently using MS Visual Studio (I used to use Turbo C++). I had started programming a couple of weeks ago. I had read up a little bit before that. I think there should be a debugging tool in Visual Studio but I haven't figured out where yet. But thanks for the input. I use this tool because my university uses it for teaching purposes. – umayneverknow Oct 01 '12 at 05:40
  • 'I know the difference between the x++ and --x' -- Nope. One should not assume that one knows the very thing that one is trying to understand. – Jim Balter Oct 01 '12 at 08:19

2 Answers2

4
void rec(int x) {
    if (x>0)
        rec(x--);
    printf("%d",x);
}

This will recurse forever (or at least until you exhaust your stack space). x-- means use the current value of xand then decrement it.

In other words, let's call rec with the parameter 20. That's greater than zero, so it will in turn call rec again with the current value of 20 then decrement x (but effectively after the call returns.

Hence you're forever calling rec with the value of 20, which is why you're blowing out your stack.

If you change x-- to --x, it decrements x before passing it to the function, hence it will go 20, 19, 18, ... 1, 0, at which point it will run back up the stack printing all those values.

If you had put a printf ("before: %d\n", x) before the if statement, you would have seen a lot of 20 lines output to the screen.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • While this certainly answers the question, I would say that the root of the problem is that ++ operators mixed with other expressions is a very effective way to create bugs. The code `if(x>0){ x--; rec(x); }` would have worked just fine. – Lundin Oct 01 '12 at 06:25
1

x-- calls the function with the old value of x (before decrement). So you will get an infinite recursion of it calling itself with the same value. --x calls the function with the new (decremented)) value of x, so it eventually hits the x > 0 condition.

Another way to investigate this is to do the following:

int a, b;
a = b = 1;

printf("--a: %d b--: %d\n", --a, b--); // Output is --a: 0 b--: 1
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72