-2

Could someone please explain to me the difference between these three blocks of code?

#include <stdio.h>

void f(int a)
{
    printf("%d", a);

    if (a >= 1)
        f(--a);

    printf("%d", a);
}

int main()
{
    f(3);
    return 0;
}

Output: 32100012

#include <stdio.h>

void f(int a)
{
    printf("%d", a);

    if (a >= 1)
        f(a - 1);

    printf("%d", a);
}

int main()
{
    f(3);
    return 0;
}

Output: 32100123

#include <stdio.h>

void f(int a)
{
    printf("%d", a);

    if (a >= 1)
        f(a--);

    printf("%d", a);
}

int main()
{
    f(3);
    return 0;
}

Output: Infinite printing of the number 3

the busybee
  • 10,755
  • 3
  • 13
  • 30
synth
  • 65
  • 4
  • For a moment there I thought you were calling `main` from inside another function. – babon Feb 01 '22 at 14:09
  • So you're asking what pre-decrement and post-decrement do? This is well documented already. – ikegami Feb 01 '22 at 14:09
  • 1
    Follow the control flow by pen and paper, and you will see. If you're brave, you can use your debugger. – the busybee Feb 01 '22 at 14:12
  • Does this answer your question? [What is the difference between ++i and i++?](https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i) – Chris Feb 01 '22 at 16:27

3 Answers3

1
    1) printf("%d", a); if (a >= 1) f(--a); printf("%d", a);
       ^^^^^^^^^^^^^^^^ ******************* ^^^^^^^^^^^^^^^^
                        *********** f(2) **

print a twice with something in between; something includes decrementing a. So f(3) prints 3<something_recursive_from_f(2)>2

    2) printf("%d", a); if (a >= 1) f(a - 1); printf("%d", a);
       ^^^^^^^^^^^^^^^^ ********************* ^^^^^^^^^^^^^^^^
                        *********** f(2) ****

print a twice with something in between. So f(3) prints 3<something_recursive_from_f(2)>3

    3) printf("%d", a); if (a >= 1) f(a--); printf("%d", a);
       ^^^^^^^^^^^^^^^^ ******************* ^^^^^^^^^^^^^^^^
                        *********** f(3) **

print a twice with something in between; something includes decrementing a. So f(3) prints 3<something_recursive_from_f(3)>2 ... but something recursive is f(3) again... and f(3) again... and f(3) again... till your computer burns out.

pmg
  • 106,608
  • 13
  • 126
  • 198
1

Third one f(a--); it is always calling f with the a == 3, then (after the function return) it will subtract 1 from a. But will never happen.

The second one - it simply calls the f with a minus 1 but not changing the a printing everything from 3 to 0 and then when the function returns the second print prints it in the reverse order as functions return in the FIFO basis.

The first one is basically very similar to the second one but a is decremented thus result printed by the second printf are 0012 not 0123

0___________
  • 60,014
  • 4
  • 34
  • 74
1

Some visual aids.

Consider what happens in the first code sample:

f(3)
-> prints "3"
-> decrements a to 2
-> calls f(2)
   -> prints "2"
   -> decrements a to 1
   -> calls f(1)
      -> prints "1"
      -> decrements a to 0
      -> calls f(0)
         -> prints "0"
         -> prints "0"
      -> prints "0"
   -> prints "1"
-> prints "2"

With the second code sample:

f(3)
-> prints "3"
-> calls f(2)
   -> prints "2"
   -> calls f(1)
      -> prints "1"
      -> calls f(0)
         -> prints "0"
         -> prints "0"
      -> prints "1"
   -> prints "2"
-> prints "3"

And the third sample code:

f(3)
-> prints "3"
-> *post*-decrements a to 2 
-> calls f(3)
   -> prints "3"
   -> *post*-decrements a to 2 
   -> calls f(3)
      -> Oops! Infinite loop.

Read up on the differences between pre- and post-increment and decrement operators.

Chris
  • 26,361
  • 5
  • 21
  • 42