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.

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

Parveen said:   1 decade ago
@Rohan..... i m 100% satisfied with rohan coz there is no matter to print 0 in answer. look at the condition..if(n>0)... then how it can enter inside the loop. Any one please explain this completely.

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:   1 decade ago
Thanx aparajita !!!

Aparajita said:   1 decade 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:   1 decade ago
There is not return statement of any kind so I think this whole code won't execute.

Remo said:   1 decade ago
Thank you imran. But this one really seems to be complicated for the beginner level. Your explanation is really helpful one.


Post your comments here:

Your comments will be displayed after verification.