5.8k views

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$

edited | 5.8k views

• Unroll recursion up to a point where we can distinguish the given options and choose the correct one!
• Options B and D are eliminated.
by Veteran (57.2k points)
selected by
+3
@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)?
+8
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.
0
yes, but in case of printing ++n is getting immediate value.

but n-2 is not getting immediate value, it printing previous value, why?
+1
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
0
where u got this property for preincrement?

Is postincrement work in same way?
+1
According to dennis ritchie

y=*ip+1

or

*ip+=1

or

++*ip

do same operation

means increment and assignment done in same time.
0
yes final result of all three expression ll be same only if pi=&y
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?

+1
@Debashish Deka

This is what we call a "GATE" mind (y)
0
(++n) => modification
0
means?

by Loyal (8k points)
0
Thank you @Arnabi for giving solution

ANS)A

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

by Active (4.8k points)

\

Solution......

by Boss (42.5k points)

by (467 points)

Ans is A

by Active (2.4k points)
0
I have another query...fun2(++n) and fun2(n++)----------- Both are same right?

Here, we won't check pre/post increment?
+1
in fun2(++n) , let take fun2(3) , value of n=3 and its ++n so first update the value of n , become n=4 and then passed as fun1(4)

but in fun2(n++) , let take same example fun2(3) , value of n=3 and its n++ so first passed value as fun1(3) then change value of n=4

( here n is local variable )

https://en.wikipedia.org/wiki/Sequence_point

regarding confusion between pre and post.

by Loyal (6.8k points)
0
@srestha
0

see , if u say ++x means

first x+1

then x=x+1

that means x is changing 2 times in between 1 sequence point

which will give undefined behaviour

So, ur assumption is not correct for ++x

++x is only increment

+1

#codeblock

0

k = ++x means pre-increment.

that means x is increased first by one then assign it k.

++n : n = n+1 or n + = 1;

but n-2 are different thing.

if n = 5

f1(n-2)  = f(5-2) = f(3) but n is still same.

but f2(++n) = f2(n = n + 1) =f2( n + = 1 ) =f2( n = 5 + 1 ) = f2( n = 6 ) = f2(6) and n=6(n is increased by 1)

also f2(n++) = f2(n = n + 1) = f2( n + = 1 )= f2(5) and n=6(n is increased by 1)

please correct me if i'm wrong??

0

In case of post increment A(x++) = A(x); x++;

Is these two operations are atomic, I mean new activation record will be created immediately after A(x) or after execution of x++;

0
after A(x) new activation record will be created.
option A is coorect
by (365 points)