+ 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! 🥺
4 Respostas
+ 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.
+ 2
JaScript Thanks a lot! It was helpful. 😊🙏☺️
+ 2
Mirielle Okay 😊 Thanks for letting me know! 😁