C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    int a=3;
    fun(a);
    return 0;
}
void fun(int n)
{
    if(n > 0)
    {
        fun(--n);
        printf("%d,", n);
        fun(--n);
    }
}
0, 2, 1, 0,
1, 1, 2, 0,
0, 1, 0, 2,
0, 1, 2, 0,
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
96 comments Page 6 of 10.

Popat said:   2 decades ago
Its correct answer Thanks Sree

Asha said:   9 years ago
Thank you guys. I understood stack.

Terry said:   2 decades ago
Parveen, I can't explain it completely but if n=1 then it can get in the if(n>0), once inside fun(--n) is called which after return from fun means n is 0 and hence zero gets printed.

Raku said:   1 decade ago
Thanks Sree bro

Saurabh said:   1 decade ago
Thanks imran it really cleared my doubt.

Rupinderjti said:   1 decade ago
Amazing sree......this is called CODING IN DEPTH.

Shivanand said:   1 decade ago
Can any one explain in simple way ?

Xyz said:   2 decades ago
Thanx aparajita !!!

Aparajita said:   2 decades ago
Whenever the condition fails the it will stop calling the recursive function but it has to come back to the next statement to be executed. In this case after the value of n=0 then it will stop calling fun(--n) but now it will pop the stack and execute the statement next to first fun(--n) function i.e. the printf statement.

Pavithra said:   2 decades ago
There is not return statement of any kind so I think this whole code won't execute.


Post your comments here:

Your comments will be displayed after verification.