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);
    }
}
0, 2, 1, 0,
1, 1, 2, 0,
0, 1, 0, 2,
0, 1, 2, 0,
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
96 comments Page 10 of 10.

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.

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)

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.

Dhananjay said:   1 decade ago
Thanks sree.

Piya said:   1 decade ago
Please any one explain in breif clearly.

Parth said:   1 decade ago
Thanks sree.


Post your comments here:

Your comments will be displayed after verification.