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 5 of 10.
Divyasree said:
1 decade ago
Its very confusing Rithesh. I can't able to undstand the flow of excecution. Can anyone explain it clearly?
Rajendra said:
1 decade ago
Thanks Sree
Malligadu said:
1 decade ago
Thank you kavita.
Mahesh Vanol said:
1 decade ago
fun(3) //upside fun() running
if(3>0)
fun(2) //upside fun() running
if(2>0)
fun(1) //upside fun() running
if(1>0)
fun(0) //upside fun() running
if(0>0) false
so return; //upside fun() finished
"print == 0"
fun(-1) //Now downside fun() running
if(-1>0) false
so return; //Downside fun() finished
"print == 1" //BCoz Inside the fun(1) block fun(0)
if(0>0) false
so return; //Downside fun() finished
"print == 2" //BCoz Inside the fun(2) block fun(1)
if(1>0)
fun(0) false
so return;
"print == 0" //BCoz Here last n=0
fun(-1)
if(-1>0) false
so return; //Downside fun() finished
OUTPUT == 0,1,2,0
if(3>0)
fun(2) //upside fun() running
if(2>0)
fun(1) //upside fun() running
if(1>0)
fun(0) //upside fun() running
if(0>0) false
so return; //upside fun() finished
"print == 0"
fun(-1) //Now downside fun() running
if(-1>0) false
so return; //Downside fun() finished
"print == 1" //BCoz Inside the fun(1) block fun(0)
if(0>0) false
so return; //Downside fun() finished
"print == 2" //BCoz Inside the fun(2) block fun(1)
if(1>0)
fun(0) false
so return;
"print == 0" //BCoz Here last n=0
fun(-1)
if(-1>0) false
so return; //Downside fun() finished
OUTPUT == 0,1,2,0
Ansh said:
1 decade ago
Can someone please explain this in the most simple manner?
RANEESH said:
1 decade ago
First of all thanks sree for explaining.
Now if we can understand how 0 is printed first then we can understand the whole program.
BRIEFED ANSWER IS IN STEP 3 MUST READ
Firstly if you can figure out this program you can understand above one.
FUNCTION A(INT N)
{
IF(N>0)
{
FUNCTION B() // calls function b
PRINT STATEMENT
}
}
1. Function b is called
2. Function b fails.
3. FUNCTION B RETURNS TO IF STRUCTURE AFTER FAILING AND THEN PRINT STATEMENT EXECUTED and 0 is printed.
4. Then further functions called.
Now if we can understand how 0 is printed first then we can understand the whole program.
BRIEFED ANSWER IS IN STEP 3 MUST READ
Firstly if you can figure out this program you can understand above one.
FUNCTION A(INT N)
{
IF(N>0)
{
FUNCTION B() // calls function b
PRINT STATEMENT
}
}
1. Function b is called
2. Function b fails.
3. FUNCTION B RETURNS TO IF STRUCTURE AFTER FAILING AND THEN PRINT STATEMENT EXECUTED and 0 is printed.
4. Then further functions called.
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.
Please explain.
Shafeeq said:
1 decade ago
Any simple method to find output of function with recursion?
Arnab said:
1 decade ago
You can arrange each function call in a block and then understand it becomes easy,
As fun(3)---->fun(2)----------->
printf(2)
fun(1)
//After encountering parentheses return to the next line of the calling function.
As fun(3)---->fun(2)----------->
printf(2)
fun(1)
//After encountering parentheses return to the next line of the calling function.
VIK said:
1 decade ago
PRINT(n=0), PRINT(n=1), PRINT(n=2), PRINT(n=0).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers