+ 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 RĂ©ponses
+ 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! đ