can anyone explain the output of the following code in c [closed]

If you really want to follow it, you can, but keep track of your call stack carefully.

main: fun(1)
main: fun(1):                                               prints 1
main: fun(1): fun(2):                                       prints 2
main: fun(1): fun(2): fun(3):                               prints 3
main: fun(1): fun(2): fun(3): fun(4) [1st call]:            prints 4
main: fun(1): fun(2): fun(3): fun(4) [1st call]:  returns 4
main: fun(1): fun(2): fun(3): fun(4) [2nd call]:            prints 4
main: fun(1): fun(2): fun(3): fun(4) [2nd call]:  returns 4
main: fun(1): fun(2): fun(3): fun(4) [3rd call]:            prints 4
main: fun(1): fun(2): fun(3): fun(4) [3rd call]:  returns 4
main: fun(1): fun(2): fun(3):                     returns 4
main: fun(1): fun(2): fun(4):                     returns 4
...

And so on… it gets a bit tedious. Perhaps you could simplify by reducing the ‘< 4’ to ‘< 2’ and have just two recursive calls to fun(). Then it would be easier to follow.

Leave a Comment