Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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_Increment_Decrement - Fatal编程技术网

C 理解递归和预减量

C 理解递归和预减量,c,increment,decrement,C,Increment,Decrement,嗨,有人能解释一下这个代码吗 #include <stdio.h> int main(){ void myfunc(int x){ printf (" [%d]",x); printf ("M here 1\n"); if (x > 0) myfunc(--x); printf ("M here 2\n"); printf (" %d,\n",x); } myfunc

嗨,有人能解释一下这个代码吗

#include <stdio.h>

int main(){

    void myfunc(int x){
        printf ("  [%d]",x);
        printf ("M here 1\n");
        if (x > 0) myfunc(--x);
        printf ("M here 2\n");
        printf ("  %d,\n",x);
    }
    myfunc(5);
}
但是,我一直在想

0,M here 2   [0]
0,M here 2   [1]
1,M here 2   [2]
2,M here 2   [3]
3,M here 2   [4]
4,M here 2   [5]
它不是应该停在那里吗

0,M here 2   [0]
它不是应该停在那里吗

0,M here 2   [0]
不,不应该。递归调用返回后,函数继续执行该行后面的行

当输入为2时,让我们看一下myfunc的执行过程。在那之后,你可以把它外推到更高的值。使用2调用函数时,您会得到:

printf ("  [%d]",2);
printf ("M here 1\n");
myfunc(1);               // Since 2 > 0
printf ("M here 2\n");
printf ("  %d,\n",1);    // You get 1 here since x is decremented.
printf ("  [%d]",2);
printf ("M here 1\n");

   printf ("  [%d]",1);
   printf ("M here 1\n");

      printf ("  [%d]",0);
      printf ("M here 1\n");
      printf ("M here 2\n");
      printf ("  %d,\n",0);

   printf ("M here 2\n");
   printf ("  %d,\n",0);

printf ("M here 2\n");
printf ("  %d,\n",1);
当用1调用函数时,得到

当使用0调用函数时,将得到

现在,如果将递归调用展平,将得到:

printf ("  [%d]",2);
printf ("M here 1\n");
myfunc(1);               // Since 2 > 0
printf ("M here 2\n");
printf ("  %d,\n",1);    // You get 1 here since x is decremented.
printf ("  [%d]",2);
printf ("M here 1\n");

   printf ("  [%d]",1);
   printf ("M here 1\n");

      printf ("  [%d]",0);
      printf ("M here 1\n");
      printf ("M here 2\n");
      printf ("  %d,\n",0);

   printf ("M here 2\n");
   printf ("  %d,\n",0);

printf ("M here 2\n");
printf ("  %d,\n",1);
很容易看出这将产生输出的原因:

  [2]M here 1
  [1]M here 1
  [0]M here 1
M here 2
  0,
M here 2
  0,
M here 2
  1,

这是您的代码,我只更改了在主函数中传递给myfunc的参数,并标记了不同的行,以便更容易解释发生了什么:

#include <stdio.h>

int main(){

  void myfunc(int x){
    printf ("  [%d]",x);      // [1]
    printf ("M here 1\n");    // [2]
    if (x > 0)                // [3]
      myfunc(--x);            // [4]
    printf ("M here 2\n");    // [5]
    printf ("  %d,\n",x);     // [6]
  }

  myfunc(2);                  // [7]
}
如果使用相应的x值获取调用的所有打印,则会得到:

[1] print 2        
[2] print 2        >   [2]M here 1
[1] print 1        
[2] print 1        >   [1]M here 1
[1] print 0        
[2] print 0        >   [0]M here 1
[5] print 0        > M here 2
[6] print 0        >   0,
[5] print 0        > M here 2
[6] print 0        >   0,
[5] print 1        > M here 2
[6] print 1        >   1,

在循环中,您必须记住,每次调用自身内部的函数时,在某个点上,该调用将返回,并且以下语句将正常执行。构建/理解递归函数的一个好方法是,不要把它看作是一组独立执行任务的语句,而是把它看作一条执行某些任务的完整指令。这样,如果您可以先编写函数的规范,您就知道如何在函数内部重用它,以及它是否安全。

-x在将其传递给myfuncyeah之前递减x,明白了。。但是我想知道,在第一个0之后x是如何递增的,这里是2[0]@notbad.jpeg:x无法传递给函数!C是严格按值传递的!首先,在从任何递归调用返回之前,它将下降到最内部的递归级别。然后它开始从内部返回到外部,颠倒传递的x值的顺序。要理解递归,你必须理解递归-Wikipedia。
[1] print 2        
[2] print 2        >   [2]M here 1
[1] print 1        
[2] print 1        >   [1]M here 1
[1] print 0        
[2] print 0        >   [0]M here 1
[5] print 0        > M here 2
[6] print 0        >   0,
[5] print 0        > M here 2
[6] print 0        >   0,
[5] print 1        > M here 2
[6] print 1        >   1,