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);
}
}
Discussion:
96 comments Page 1 of 10.
Subu said:
1 decade ago
Please explain this.
Sree said:
1 decade ago
// typedef int (*pf) (int, int);
// int proc(pf, int, int);
The above two statement has no effects , because they are not used in main or sub function.
Function calls are stored in stack, that is LAST IN FIRST OUT.
1.fun(a=3)---calling sub function--->fun(n=3).
2.fun(3)->if(3>0)true{fun(2)-->step 3,print(n),fun(--n)//active}
3.fun(2)->if(2>0)true{fun(1)-->step 4,print(n),fun(--n)//active}
4.fun(1)->if(1>0)true{fun(0)-->step 5,print(n),fun(--n)//active}
5.fun(0)->if(0>0)false;
6.From step 4,{fun(n=0)//finished,PRINT(n=0),fun(-1)-->step 7}
7.fun(-1)->if(-1>0)false;
8.From step 3,{fun(n=1)//finished,PRINT(n=1),fun(0)-->step 9}
9.fun(0)->if(0>0)false;
10.From step 2,{fun(n=2)//finished,PRINT(n=2),fun(1)-->step 11}
11.fun(1)->if(1>0)true{fun(0)->step 12,print(n),fun(--n)//acti}
12.fun(0)->if(0>0)false;
13.From 11,{fun(n=0)//finished,PRINT(0),fun(-1)-->step 14}
14.fun(-1)->if(-1>0)false;
// all active calls are called and removed from stack.
// here 'n' is a local variable for each function.
// The printing statements are capitalized
PRINT(n=0),PRINT(n=1),PRINT(n=2),PRINT(n=0).
// int proc(pf, int, int);
The above two statement has no effects , because they are not used in main or sub function.
Function calls are stored in stack, that is LAST IN FIRST OUT.
1.fun(a=3)---calling sub function--->fun(n=3).
2.fun(3)->if(3>0)true{fun(2)-->step 3,print(n),fun(--n)//active}
3.fun(2)->if(2>0)true{fun(1)-->step 4,print(n),fun(--n)//active}
4.fun(1)->if(1>0)true{fun(0)-->step 5,print(n),fun(--n)//active}
5.fun(0)->if(0>0)false;
6.From step 4,{fun(n=0)//finished,PRINT(n=0),fun(-1)-->step 7}
7.fun(-1)->if(-1>0)false;
8.From step 3,{fun(n=1)//finished,PRINT(n=1),fun(0)-->step 9}
9.fun(0)->if(0>0)false;
10.From step 2,{fun(n=2)//finished,PRINT(n=2),fun(1)-->step 11}
11.fun(1)->if(1>0)true{fun(0)->step 12,print(n),fun(--n)//acti}
12.fun(0)->if(0>0)false;
13.From 11,{fun(n=0)//finished,PRINT(0),fun(-1)-->step 14}
14.fun(-1)->if(-1>0)false;
// all active calls are called and removed from stack.
// here 'n' is a local variable for each function.
// The printing statements are capitalized
PRINT(n=0),PRINT(n=1),PRINT(n=2),PRINT(n=0).
(2)
Kavita said:
1 decade ago
Thanks Sree.
Lakshmi said:
1 decade ago
Sree i didnt get wat u r saying.Plz can u explain me in brief.
Bagesh kumar bagi said:
1 decade ago
Any one explain again?
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.
Popat said:
1 decade ago
Its correct answer Thanks Sree
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.
How can it execute the printf statement when the condition in if goes false at step 5. Can you explain please.
Sumit Pankaj said:
1 decade ago
Please explain once more
Rakesh said:
1 decade ago
Sree is absolutely right. Nice one. Good logic.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers