unexpected output in C (recursion)

前端 未结 3 1607
迷失自我
迷失自我 2020-11-28 00:03
int main(void) {
 static int=5;
 if(--i) {
    main();
    printf(\"%d\",i);
   }
 }

the output of above program is---

0000

But I t

3条回答
  •  情歌与酒
    2020-11-28 00:42

    The reason for the zeros is that i is decremented down to zero before the very first printf statement is run. As it unwinds, it prints i (which is still zero) each time.

    It would be better to use a separate function that main() calls and passes a parameter to (and then pass the parameter to each call rather than using a static variable).

提交回复
热议问题