in Programming in C edited by
20,251 views
47 votes
47 votes

Consider the following two functions.

void fun1(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun2(n - 2);
    printf("%d", n);
}
void fun2(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun1(++n);
    printf("%d", n);
}

The output printed when $\text{fun1}(5)$ is called is

  1. $53423122233445$  
  2. $53423120112233$                       
  3. $53423122132435$  
  4. $53423120213243$                       
in Programming in C edited by
by
20.3k views

4 Comments

Whenever we take a static variable, until function calls are over we won't print the value i.e. we take the last updated value after the function call and then we print it,but if the variable is global then why do we directly take the value and print it .​​?Here second printf stmt i.e.stmt after function call we fixed the value into the variable and it's been printed at the end.why? 

0
0
Yeah got it bro.And the video is primarily focussed on how to use options to get the correct solution
0
0
Mistakes we might do in the question are like not considering pre-increment.
0
0

11 Answers

98 votes
98 votes
Best answer

  • Unroll recursion up to a point where we can distinguish the given options and choose the correct one!
  • Options B and D are eliminated.
  • A is the answer.
edited by
by

13 Comments

@Debashish could u tell me what is the difference between

if we replace fun1(++n) with fun1(n+1) ?

and also fun1(n+1) with fun1(n-1)?
3
3
https://gateoverflow.in/118319/gate2017-1-36
let n=3, then fun1(++n) is equivalent to call fun1(4) and at the time value of n get modified to n=4.
whereas fun1(n+1) is equivalent to call fun1(4) but value of n will not get effected i.e ll remains same to n=3.

in 2nd part, fun1(n+1) is just equal to call fun1(4)  and fun1(n-1) is equivalent to fun1(2),
in this case for both value of n will remains same i.e n=3.
12
12
yes, but in case of printing ++n is getting immediate value.

but n-2 is not getting immediate value, it printing previous value, why?
0
0
bcz ++n is equivalent to write n=n+1(increment+assign at same time)
where as n-2 is not equivalent to say n=n-2
n-2 calculated at the time but it result will not assigned to it you can use that intermediate for sort duration(at that time only) after that it automatically erased
1
1
where u got this property for preincrement?

Is postincrement work in same way?
0
0
According to dennis ritchie

y=*ip+1

or

*ip+=1

or

++*ip

do same operation

 

means increment and assignment done in same time.
1
1
yes final result of all three expression ll be same only if pi=&y
0
0

https://gateoverflow.in/118319/gate2017-1-36

here first assign then modify the value in foo() function call.

that is why it goes to infinite loop rt?

0
0
@Debashish Deka

 

#Answer

This is what we call a "GATE" mind (y)
3
3
(++n) => modification
0
0
means?
0
0
please anyone can tell me that why right side printf value is incremented
0
0
pre-increment and for every function call there’ll be activation record which will store the local variable values .So, at the time of fun call ,variable value has been incremented .It was stored and that’s the reason why it has been incremented.
1
1
54 votes
54 votes

hence correct answer is a.

4 Comments

Thank you :)
0
0
please anyone can tell me that why right side printf value is incremented
0
0

in $f_2()$ after the printf statement there is a pre-increment of variable $n$.

1
1
11 votes
11 votes

ANS)A

In fun2,value of n also get incremented after each function call.

1 comment

please anyone can tell me that why right side printf value is incremented
0
0
8 votes
8 votes

\

Solution......

1 comment

please anyone can tell me that why right side printf value is incremented
0
0
Answer:

Related questions