Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图理解一些简单的递归C代码_C - Fatal编程技术网

试图理解一些简单的递归C代码

试图理解一些简单的递归C代码,c,C,有人能告诉我在输出语句“之前x的值为8”之后,流是如何进行的吗 当您有一个递归函数时,递归调用后的语句被推送到堆栈中 因此sum(x+1)之后的语句是printf(),它被推送到堆栈中,并在您从函数返回时检索 当您调用sum(2+1) 被推到堆栈中。因此,最后一次推送将是printf(),其中x=8对sum(i)的每次调用都将替换为 print value of x before is i call sum(i + 1) print value of x after is i print va

有人能告诉我在输出语句“之前x的值为8”之后,流是如何进行的吗


当您有一个递归函数时,递归调用后的语句被推送到堆栈中

因此
sum(x+1)
之后的语句是
printf()
,它被推送到堆栈中,并在您从函数返回时检索

当您调用
sum(2+1)

被推到堆栈中。因此,最后一次推送将是
printf()
,其中
x=8

sum(i)
的每次调用都将替换为

print value of x before is i
call sum(i + 1)
print value of x after is i
print value of x before is i

print value of x before is i + 1
call sum(i + 2)
print value of x after is i + 1

print value of x after is i

. . .
然后被替换为

print value of x before is i
call sum(i + 1)
print value of x after is i
print value of x before is i

print value of x before is i + 1
call sum(i + 2)
print value of x after is i + 1

print value of x after is i

. . .
直到我达到值8。借助打印语句,它是不言自明的。

请看以下代码:

#include<stdio.h>

void p(int x) {
    while(x--) putchar(' ');
}

void sum(int x)
{
    if(x==9)
        return;

    p(x); printf("value of x before is %d\n",x);

    /* recursive calling of Sum() function */

    sum(x+1);

    p(x); printf("value of x after is %d\n",x);
}

int main()
{    
    sum(2);
}
这意味着函数调用的行为如下所示:

 sum(2)
 2  is 2 == 9? NO
 2  print before
 2  sum(3)
 2  3   is 3 == 9? NO
 2  3   print before
 2  3   sum(4)
 2  3   4   is 4 == 9? NO
 2  3   4   print before
 2  3   4   sum(5)
 2  3   4   5   is 5 == 9? NO
 2  3   4   5   print before
 2  3   4   5   sum(6)
 2  3   4   5   6   is 6 == 9 ? NO
 2  3   4   5   6   print before
 2  3   4   5   6   sum(7)
 2  3   4   5   6   7   is 7 == 9 ? NO
 2  3   4   5   6   7   print before
 2  3   4   5   6   7   sum(8) 
 2  3   4   5   6   7   8       is 8 == 9 NO
 2  3   4   5   6   7   8       print before
 2  3   4   5   6   7   8       sum(9)
 2  3   4   5   6   7   8       9   is 9 == 9? YES 
 2  3   4   5   6   7   8       9   RETURN  
 2  3   4   5   6   7   print after
 2  3   4   5   6   7   RETURN
 2  3   4   5   6   print after
 2  3   4   5   6   RETURN
 2  3   4   5   print after
 2  3   4   5   RETURN
 2  3   4   print after
 2  3   4   RETURN
 2  3   print after
 2  3   RETURN
 2  print after
 2  RETURN

 main continues here
前导数字表示您正在使用的函数“which”
sum()。它是该函数中
x
的值

此外,请注意,值为3的函数位于值为2的函数的“内部”。值为4的函数位于值为3的函数的“内部”

我之所以说内部,是因为后续函数调用的堆栈位于调用方函数的堆栈框架内。但是,这并不意味着递归调用的函数可以访问调用方的数据。要做到这一点,必须像任何其他函数调用一样使用指针

$ ./rec
  value of x before is 2
   value of x before is 3
    value of x before is 4
     value of x before is 5
      value of x before is 6
       value of x before is 7
        value of x before is 8
        value of x after is 8
       value of x after is 7
      value of x after is 6
     value of x after is 5
    value of x after is 4
   value of x after is 3
  value of x after is 2
 sum(2)
 2  is 2 == 9? NO
 2  print before
 2  sum(3)
 2  3   is 3 == 9? NO
 2  3   print before
 2  3   sum(4)
 2  3   4   is 4 == 9? NO
 2  3   4   print before
 2  3   4   sum(5)
 2  3   4   5   is 5 == 9? NO
 2  3   4   5   print before
 2  3   4   5   sum(6)
 2  3   4   5   6   is 6 == 9 ? NO
 2  3   4   5   6   print before
 2  3   4   5   6   sum(7)
 2  3   4   5   6   7   is 7 == 9 ? NO
 2  3   4   5   6   7   print before
 2  3   4   5   6   7   sum(8) 
 2  3   4   5   6   7   8       is 8 == 9 NO
 2  3   4   5   6   7   8       print before
 2  3   4   5   6   7   8       sum(9)
 2  3   4   5   6   7   8       9   is 9 == 9? YES 
 2  3   4   5   6   7   8       9   RETURN  
 2  3   4   5   6   7   print after
 2  3   4   5   6   7   RETURN
 2  3   4   5   6   print after
 2  3   4   5   6   RETURN
 2  3   4   5   print after
 2  3   4   5   RETURN
 2  3   4   print after
 2  3   4   RETURN
 2  3   print after
 2  3   RETURN
 2  print after
 2  RETURN

 main continues here