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

Siva pavan said:   1 decade ago
All are explaining according to the answer. If condition in the if loop fails it completely comes out of it then how can printf statement present in the if loop executes?

Wikiok said:   1 decade ago
If the program returns from the first "fun(--n)" function it will continue running to printf statement, and then the other fun(--n) statement comes.

Kowsik said:   8 years ago
When fun(0) is called 0>0 condition fail and it will not enter in the loop further. So the program will not provide any output. Correct me if I am wrong.

Naseema said:   9 years ago
@Imran Mohammed,
I can't understand how does it prints(1) when the fun(-1) condition fails (in 10 & 11 steps).
Can someone explain it to me?

Fenil said:   8 years ago
Can anyone explain in brief?

As each time function is called & control is passed to function how printf statement will be executed?

ABHI_027 said:   1 decade ago
How can it be printed as recursive function is executed repeatedly calling & at last the program is ended with false statements?

Malavika said:   8 years ago
Please, can anyone explain clearly? because after printing 0 then the value becomes -1. After the process, I did not understand.

Rishabh said:   1 decade ago
In step 5 the condition of if gets failed. Means it doesn't enter in its body. So how the output will generate?

Please explain.

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.

Anusha said:   1 decade ago
@SREE:.

How can it execute the printf statement when the condition in if goes false at step 5. Can you explain please.


Post your comments here:

Your comments will be displayed after verification.