c - Why this function is running in an infinite loop? -


i trying learn recursion. dont understand why following piece of code runs in infinite loop?

void myfunc(int n) {      if(n==0)          return;      else      {          printf("%d\n",n);          myfunc(n--); //if put n=n-1 before line running fine , exit function .          printf("%d\n",n);       }  }  int main() {      myfunc(4); } 

-- post-decrement operator using , takes effect after myfunc called value of n, calling myfunc same value on , on again.

using pre-decrement operator fix particular use case: myfunc(--n) have similar effect putting n=n-1 on line before.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -