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 3 of 10.

Vishal said:   1 decade ago
if(3>0) true.
step:1 fun(2).
if(2>0) true.
step:2 fun(1).
if(1>0) true.
step:3 fun(0).
if(0>0) false.

print 0 from step:3, fun(-1) false.
print 1 from step:2, fun(0) false.
print 2 from step:1, fun(1)->fun(0).
print 0 from fun (0) at second last line.

Rohan said:   1 decade ago
Plz explain sree. I may be wrong.

But condition (if 3>0)okk then function executed

fun(--3)=fun(2) okk no return statement so printf will execute

So value of n will be ...n=2 so n will be print 2 first m not understanding how 0 is printed.

Please explain.

Mohan said:   6 years ago
I have a doubt when the functions are called recursively each time n value decremented by 1 hence finally it comes 0 so now n value in fun (0) is 0 so now n=0 then how it prints 1 2 in fun (1) fun (2) because here also n must be 0 how it happens? Please tell me.
(6)

Rupinder said:   1 decade ago
I think this answer is wrong becouse Printf statement will never executed.this is a recursive function and function all itself again and again in if statement.wen if condition false then it stop execution.this output is wrong.

Debalina said:   1 decade ago
@Vishal and @Sree I don't really get it for step 3.

In step 3 it should not print anything rather exit from the function, so how will zero be printed?

After all the print statement is within the if block?

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.

Nikk said:   7 years ago
If 3>0 then --it will call fun(2) & fun(2) call fun(1)& fun(1) call fun(0)
then fun(0)returns 0 to fun(1),it will print(0), After that how the flows goes?

Please explain it.

Terry said:   1 decade 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.

Rupinderjit Singh said:   1 decade ago
@Rathika.B: Well your query is genuine and it is compiler dependent.
And It is not mentioned in standard C, that, in what order sequence will execute.
So it's answer is UNDEFINED.

Mukunda saini said:   7 years ago
According to me, It is;

fun(3)->fun(2) -> fun(1) -> fun(0)
print 2 print 1 print 0 finish
fun(1)-> fun(0) fun(-1)
print 0 finish finish
finish
(1)


Post your comments here:

Your comments will be displayed after verification.