C 中的意外输出(递归) [英] unexpected output in C (recursion)

查看:22
本文介绍了C 中的意外输出(递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(void) {
 static int=5;
 if(--i) {
    main();
    printf("%d",i);
   }
 }

上面程序的输出是---

the output of above program is---

0000

但我认为应该是---

1234

我不知道为什么?请帮帮我.

I dont know why?please help me.

推荐答案

零的原因是 i 在第一个 printf 语句之前递减为零正在运行.当它展开时,它每次都打印 i(仍然为零).

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.

最好使用一个单独的函数,main() 调用并将参数传递给(然后将参数传递给每个调用,而不是使用静态变量).

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).

这篇关于C 中的意外输出(递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆