+ 2

Why there is an infinite recursion? Please help! (Code below)

#include <stdio.h> int foo(int num) { if(num == 0) return 0; else printf("%d,", num); return foo(num--); } int main() { foo(4); return 0; } I am not getting that why this program's output is infinite? Can anyone explain me? Please help! 🥺

13th Jan 2021, 7:23 PM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
4 Respuestas
+ 4
num— is post decrementing of the value. That means first will be a operation done and after that the value decremented. Formal parameters behave within the function similarly to other local variables. They are created upon entering the function, and are destroyed upon exiting the function. So in above mentioned sytuation the function foo get each time the 4 and will be executed. The solution is —num.
13th Jan 2021, 7:53 PM
JaScript
JaScript - avatar
+ 2
JaScript Thanks a lot! It was helpful. 😊🙏☺️
14th Jan 2021, 3:49 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 2
Mirielle Okay 😊 Thanks for letting me know! 😁
14th Jan 2021, 3:50 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar