C language recursion control -
the program function recursion, need know how control entire program
void count(int n) { static int d=1; printf("%d",n); printf("%d",d); d++; if(n>1) count(n-1); printf("%d",d); } void main() { count(3); }
my expected output
3122134
original output is
312213444
can please explain how 2 4's has added output?
please explain flow control program.
here you're friendly drawing understand why happens (the static
not problem because update variable static int d
, doing d++
inside function):
when call recursion inside function, count(2)
starts, count(3)
not destroyed, sleeping, waiting subfunction count(2)
finish. when happens, count(3)
wakes , says "oh, finally! daughter count(2)
has finished, it's turn finish". remember that, inside count(2)
, call count(1)
...
Comments
Post a Comment