177 177 votes Consider the C functions foo and bar given below: int foo(int val) { int x=0; while(val > 0) { x = x + foo(val--); } return val; } int bar(int val) { int x = 0; while(val > 0) { x= x + bar(val-1); } return val; } Invocations of $foo(3)$ and $bar(3)$ will result in: Return of $6$ and $6$ respectively. Infinite loop and abnormal termination respectively. Abnormal termination and infinite loop respectively. Both terminating abnormally. Programming in C gatecse-2017-set1 programming-in-c programming normal recursion + – Arjun 42.2k views answer comment Share Follow Print See all 21 Comments 21 21 Comments reply Show 18 previous comments Patel_And_Patel commented Jun 9 1 flag: ✌ Low quality (panipuri “everyone has chatgpt access”) reply Follow flag Let's analyze both functions carefully.Function foo int foo(int val) { int x = 0; while(val > 0) { x = x + foo(val--); } return val; }The key point is post-decrement: foo(val--)passes the current value of val to the recursive call, and only then decrements val.Suppose we call: foo(3)Inside the loop: foo(val--)becomes foo(3)and only after the call returns would val become 2.But the call is foo(3) again, which immediately calls foo(3) again, and so on.Thus the recursion never progresses toward a base case.Result: infinite recursion → stack overflow → abnormal termination.Function bar int bar(int val) { int x = 0; while(val > 0) { x = x + bar(val - 1); } return val; }For bar(3): while(val > 0)and notice that val is never modified inside the loop.The recursive call: bar(val - 1)eventually reaches: bar(0)which returns normally.However, after that return, we are still inside: while(val > 0)with val == 1 (or 2, or 3 depending on the level), and val never changes.Therefore the loop runs forever.Result: infinite loop.Conclusionfoo(3) → abnormal termination (infinite recursion / stack overflow)bar(3) → infinite loopAnswer: C. Abnormal termination and infinite loop respectively. 2 2 replyShare Honey badger commented Jul 3 reply Follow flag Taking val=1 ; makes this loop work infinite, basically bar(0) will be called again and again , we are not calling bar(1) multiple times, it is while loop which is calling bar(0) again and again. while(1> 0) { //val=1 x= x + bar(val-1); } 0 0 replyShare Hardik Kumawat commented Sep 18 reply Follow flag $$\text{bar(3)}$$ 0 0 replyShare Please log in or register to add a comment.
0 0 votes while(val>0) { x = x + foo(val--); } In this case foo(val--) is same as foo(val) & val-- ; Because the recursive function call is made without changing the passing argument and there is no Base condition which can stop it. It goes on calling with the same value ‘val’ & the system will run out of memory and hits the segmentation fault or will be terminated abnormally. The loop will not make any difference here. while(val>0) { x = x + bar(val-1); } bar(3) calls bar(2) bar(2) calls bar(1) bar(1) calls bar(0) ⇾ Here bar(0) will return 0. bar(1) calls bar(0) bar(1) calls bar(0)…….. This will continue. Here is a problem of infinite loop but not abrupt termination. Some compilers will forcefully preempt the execution. varunrajarathnam answered Dec 11, 2020 varunrajarathnam comment Share Follow See 1 comment 1 1 comment reply Khushbu makode commented Apr 30, 2024 reply Follow flag thanks got it 0 0 replyShare Please log in or register to add a comment.
0 0 votes Hello use this link Video Solution: https://www.youtube.com/watch?v=fW_vmo8A3Lg&ab_channel=JatinGupta go_rajesh answered Jan 14, 2025 go_rajesh comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes https://youtu.be/uB_vN8tMORw?feature=shared best answer found Rohit Gupta 62 answered Aug 17, 2025 Rohit Gupta 62 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes YOUTUBE LINKGate 2017 pyq C Programming | Consider the C functions foo and bar given below:int foo(int val) shubhamkrgate1 answered Dec 15, 2025 shubhamkrgate1 comment Share Follow 0 reply Please log in or register to add a comment.