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

时间:2015-09-04 10:13:13

标签: c

在输出语句“x之前的值是8”之后,有人能告诉我流程是怎么回事吗?

#include<stdio.h>

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

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

    /* recursive calling of Sum() function */

    sum(x+1);

    printf("\nvalue of x after is %d",x);
}

int main()
{    
    sum(2);
}

输出:

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

3 个答案:

答案 0 :(得分:1)

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

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

致电sum(2+1)

printf("\nvalue of x after is %d",x);/* x =2 */

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

答案 1 :(得分:1)

每次拨打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

. . .

直到我达到值8.在print语句的帮助下,它是不言自明的。

答案 2 :(得分:0)

看看这段代码:

#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);
}

将输出:

$ ./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

前导数字表示您的“{”sum()功能。它是该函数内x的值。

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

我在里面说,因为后续函数调用的stack frames存在于调用函数的堆栈框架内。但是,此并不意味着递归调用函数可以访问其调用者的数据。为此,您必须像使用任何其他函数调用一样使用指针。