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 7 of 10.
Biru said:
9 years ago
Not able to understand it. Can anyone help me?
Priya said:
9 years ago
I can't understand. Please help me.
Lakshmi said:
9 years ago
I can't understand. Please explain this.
Vinesh Chauhan said:
9 years ago
Great logic @Sree and other friends who explain this.
Actually, when first time fun (3); is executed the control transfer to int fun (int n) right.
Then in side of the definition of fun there is another fun() which is fun(--n). So the value of n=2 then again called fun (2) this loop is condition until value of n is equal to 0 then the control passes to printf() because when ever recursion of the function proceeds after that the control return to that point from where it called that's why printf will called value with 0.
Actually, when first time fun (3); is executed the control transfer to int fun (int n) right.
Then in side of the definition of fun there is another fun() which is fun(--n). So the value of n=2 then again called fun (2) this loop is condition until value of n is equal to 0 then the control passes to printf() because when ever recursion of the function proceeds after that the control return to that point from where it called that's why printf will called value with 0.
Surya mitthu said:
9 years ago
I don't understand this. Please someone explain this to me.
Abhijit Dey said:
8 years ago
Good explanation, Thanks @Sree.
Manu said:
8 years ago
I got it as 0,1,0,2,0,1,0.
How? Please tell me.
How? Please tell 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?
As each time function is called & control is passed to function how printf statement will be executed?
Vidya said:
8 years ago
Well said @Sree. Thanks.
Manjunath Rk said:
8 years ago
Good explaination, Thanks @Sree.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers