0

Can anyone tell me the reason of getting 0 1 2 0 as output of below program?

#include <stdio.h>

main() {

    e(3);

}

void e(int n) {

    if(n>0) {
        e(--n);
        printf("%d",n);
        e(--n);
    }

}

Output is 0 1 2 0

6
  • 3
    I wonder more how you got the code even running! Call a function which isn't known at compile time?! Commented Mar 23, 2015 at 17:21
  • 3
    Your code is ill-formed, you don't declare the main function properly. It must return an int, and as argument either take void or the usual arg/argv pair. Commented Mar 23, 2015 at 17:22
  • 3
    As for your problem, try to "run" the code on paper, or at the very least step through it line by line in a debugger. Commented Mar 23, 2015 at 17:23
  • This question came to my mind also but the output of the code is troubling me a lot .If you know the reason ,do share it with me . Commented Mar 23, 2015 at 17:25
  • 1
    The purpose of the question was to see if you understand something called recursion. Read up on that and the answer should become clear. Commented Mar 23, 2015 at 17:35

4 Answers 4

4

Here' the flow of execution after e(3) is called from main.

e(3)
   e(2) 
      e(1) 
         e(0) ... return
         n is now 0
         print n. results in output 0
         e(-1) ... return
      n is now 1
      print n. results in output 1
      e(0) ... return
   n is now 2
   print n. results in output 2
   e(1) 
      e(0) ... return
      n is now 0
      print n. results in output 0
      e(-1) ... return
   return

And you see the output

0 1 2 0
Sign up to request clarification or add additional context in comments.

1 Comment

@VartikaSingh - don't be shy about up clicking, or clicking answered on an answer if it helps you. (little up and down arrows to the upper left of the answer)
2

I'm assuming the following is what you want:

#include <stdio.h>

void e(int);

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

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

This is an example of a recursive function - a function calling itself. Here, at each call the parameter is decremented and the function is again called until the condition n > 0 is not met. Then, the printf("%d", 0) happens. Now the second e(--n) will have no effect until n is at least 2, since the if condition cannot be passed with a value of n less than 1. Further printf()s happen in the reverse order of the call as the function calls are removed from the stack. When the value gets to 2, the second e(--n) gets a chance to make an effect thus printing 0.

You need to learn about recursion (if you still haven't) and then you can get a good picture of how things happen. Also, it will help you if learn more about how the stack is set up when a function is called, and later returned.

Comments

0

The 'flow' goes as follows:

main -> e(3)
e(3) -> IF(3>0)
{
    // n is pre-decremented to 2
    e(2) -> IF(2>0)
    {
        // n is pre-decremented to 1
        e(1) -> IF(1>0)
        {
            // n is pre-decremented to 0
            e(0) -> 0 is not > 0 so this call does nothing.
            // n is still 0 in this function call so...
            printf(0)  <-- The first '0' in the output
            // n is pre-decremented to -1
            e(-1) -> -1 is not > 0) so this call does nothing.
        }
        // n is still 1 in this function call so...
        printf(1)  <-- The '1' in the output
        // n is pre-decremented to 0
        e(0) -> 0 is not > 0 so this call does nothing
    }
    // n is still 2 in this function call so...
    printf(2)  <-- The '2' in the output
    // n is pre-decremented to 1
    e(1) -> (1 is > 0)
    {
        // n is pre-decremented to 0
        e(0) -> 0 is not > 0 so this call does nothing
        // n is still 0 in this function call so...
        printf(0)  <-- The second '0' in the output
        // n is pre-decremented to -1
        e(-1) -> -1 is not > 0 so this call does nothing
    }
}

It helps if you set the code out more clearly:

#include<stdio.h>

main()
{
    e(3);
}

void e(int n)
{
    if(n>0)
    {
        e(--n);    //  First recursion here, but with n=n-1 on entry to the call.
        printf("%d",n);  // outputs (original value of n) - 1.
        e(--n);    // Second recursion here, now with n=n-2 on entry to the call.
    }
}

Comments

0

After denesting the code the reason for the results can be deduced in a single run in a debugger.

e() is recursive and called once before the print and once after. So before you hit your print statement you'll have to go through e again, and again, and again till it finally hits 0.

After that things start unlooping and you'll see prints popping up but it's still a big recursive mess because of the second call to e(n) in which n dips into the negative. I was rather grateful n was signed because if it was unsigned it would loop round to 2^32 and the program would get stuck in, pretty much, an infinite loop.

So yeah, TL;DR: run it through a debugger and learn from the FUBAR a recursion like this can cause.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.