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 4 of 10.
Parth said:
1 decade ago
Thanks sree.
Piya said:
1 decade ago
Please any one explain in breif clearly.
Dhananjay said:
1 decade ago
Thanks sree.
Rupinderjit Singh said:
1 decade ago
@Rathika.B: Well your query is genuine and it is compiler dependent.
And It is not mentioned in standard C, that, in what order sequence will execute.
So it's answer is UNDEFINED.
And It is not mentioned in standard C, that, in what order sequence will execute.
So it's answer is UNDEFINED.
Shailendra said:
1 decade ago
fun(3)-1. fun(2)----1.fun(1)-----1.fun(0)
2. printf 2 2.printf 1 2.printf 0
3. fun(1) 3.fun(0) 3.fun(0)
So at first 1. step will execute. fun(3)->fun(2)->fun(1)->fun(0)=0
2.pf 0 -> 3. fun(0)=0 so print 0
Then it will return last statement fun(1).... 1. completed
2. pf 1
3.fun(0)
Now it will return at fun(2)-- 1. completed
2. pf 2
3. fun(1)-->1.fun(0)
2.pf 0
3.fun(0)
2. printf 2 2.printf 1 2.printf 0
3. fun(1) 3.fun(0) 3.fun(0)
So at first 1. step will execute. fun(3)->fun(2)->fun(1)->fun(0)=0
2.pf 0 -> 3. fun(0)=0 so print 0
Then it will return last statement fun(1).... 1. completed
2. pf 1
3.fun(0)
Now it will return at fun(2)-- 1. completed
2. pf 2
3. fun(1)-->1.fun(0)
2.pf 0
3.fun(0)
Ritesh said:
1 decade ago
Lets change the same program bit differently for understanding the concept properly.....
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a); // here the value of a is 3 which is passed to n
return 0;
}
void fun(int n) //here the value obtained is 3 so n=3
{
if(n > 0) //condition is 3>0 hence true, enters the loop
{
fun1(--n); //here value of n = 2 and calls function fun1
printf("%d,", n); //prints 2 after the call function is returned
fun4(--n); //here value again becomes 1
}
}
void fun1(int m) //here the value obtained is 2 so m=2
{
if(m > 0) //condition is 2>0 hence true, enters the loop
{
fun2(--m); //here value of m = 1 and calls function fun2
printf("%d,", m); //prints 1 after the call function is returned
fun5(--m); //here value becomes 0
}
}
void fun2(int p) //here the value obtained is 1 so n=1
{
if(p > 0) //condition is 1>0 hence true, enters the loop
{
fun3(--p); //here value of p = 0 and calls function fun3
printf("%d,", p); //prints 0 after the call function is returned
fun4(--p); //here value again becomes -1
}
}
void fun3(int q) //here the value obtained is 0 so n=0
{
if(q > 0) //condition is 0>0 hence false, func ends and goes back to fun2 func to printf statement.
{
same codes here which are not executed....
}
}
So if u see the values printed here is 0 from fun2 then 1 from fun1 and 2 from fun.... as u can see if the input given to func is 3 it prints 2, if input is 2 then it prints 1 and when input is 1 it prints 0. So when fun2 is printed and the value of p becomes -1 it prints nothing and when it prints 1 from fun1 value of m becomes 0 and prints nothing. But when the value 2 is printed in fun, the value of n becomes 1 so it prints 0 again.
So the output will be :
0,1,2,0,
All the option given in above problem is wrong because, after every no is printed a "," comma should be printed as per the program. But in the option 'D', comma is not given after last zero. In programming even a comma matters a lot.
In the above program all the variables that i declared are taken same and function is called repeatedly instead of calling it with different names like i did.. I just gave diff names so that u will understand the flow. I hope u no the flow of the program when the function is called. I hope u understood the logic. Thank you.
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a); // here the value of a is 3 which is passed to n
return 0;
}
void fun(int n) //here the value obtained is 3 so n=3
{
if(n > 0) //condition is 3>0 hence true, enters the loop
{
fun1(--n); //here value of n = 2 and calls function fun1
printf("%d,", n); //prints 2 after the call function is returned
fun4(--n); //here value again becomes 1
}
}
void fun1(int m) //here the value obtained is 2 so m=2
{
if(m > 0) //condition is 2>0 hence true, enters the loop
{
fun2(--m); //here value of m = 1 and calls function fun2
printf("%d,", m); //prints 1 after the call function is returned
fun5(--m); //here value becomes 0
}
}
void fun2(int p) //here the value obtained is 1 so n=1
{
if(p > 0) //condition is 1>0 hence true, enters the loop
{
fun3(--p); //here value of p = 0 and calls function fun3
printf("%d,", p); //prints 0 after the call function is returned
fun4(--p); //here value again becomes -1
}
}
void fun3(int q) //here the value obtained is 0 so n=0
{
if(q > 0) //condition is 0>0 hence false, func ends and goes back to fun2 func to printf statement.
{
same codes here which are not executed....
}
}
So if u see the values printed here is 0 from fun2 then 1 from fun1 and 2 from fun.... as u can see if the input given to func is 3 it prints 2, if input is 2 then it prints 1 and when input is 1 it prints 0. So when fun2 is printed and the value of p becomes -1 it prints nothing and when it prints 1 from fun1 value of m becomes 0 and prints nothing. But when the value 2 is printed in fun, the value of n becomes 1 so it prints 0 again.
So the output will be :
0,1,2,0,
All the option given in above problem is wrong because, after every no is printed a "," comma should be printed as per the program. But in the option 'D', comma is not given after last zero. In programming even a comma matters a lot.
In the above program all the variables that i declared are taken same and function is called repeatedly instead of calling it with different names like i did.. I just gave diff names so that u will understand the flow. I hope u no the flow of the program when the function is called. I hope u understood the logic. Thank you.
Maggi said:
1 decade ago
Thank you rithesh. Good explanation. :).
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
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.
Jhunu said:
1 decade ago
Thanks Ritesh. You are excellent.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers