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 9 of 10.
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.
Ansh said:
1 decade ago
Can someone please explain this in the most simple manner?
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
Malligadu said:
1 decade ago
Thank you kavita.
Rajendra said:
1 decade ago
Thanks Sree
Popat said:
1 decade ago
Its correct answer Thanks Sree
Jhunu said:
1 decade ago
Thanks Ritesh. You are excellent.
Saidattu G said:
1 decade ago
void fun(int n) is a function
and in this function we again calling fun(--n)
Means it is a recursive function
Here the condition if(n>0) again check
This works as a loop till condition is false
That means till i=0
Then it prints 0
After print again we are calling fun(--n)
Now 2 loops have to execute so
Finally it prints 120.
and in this function we again calling fun(--n)
Means it is a recursive function
Here the condition if(n>0) again check
This works as a loop till condition is false
That means till i=0
Then it prints 0
After print again we are calling fun(--n)
Now 2 loops have to execute so
Finally it prints 120.
Shashi said:
1 decade ago
I will try to explain:
First note that for these things:
1. fun(0) will not execute.
2. fun(1) will only print 0 i.e. --n.
Now follow the tree
fun(2)----> fun(1) -----> print 0
print 1
print 2
fun(1)----> print 0
So output would be 0 1 2 0
First note that for these things:
1. fun(0) will not execute.
2. fun(1) will only print 0 i.e. --n.
Now follow the tree
fun(2)----> fun(1) -----> print 0
print 1
print 2
fun(1)----> print 0
So output would be 0 1 2 0
Maggi said:
1 decade ago
Thank you rithesh. Good explanation. :).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers