程序的输出
#include<stdio.h>
void f(void)
{
int s = 0;
s++;
if(s == 10)
return;
f();
printf("%d ", s);
}
int main(void)
{
f();
}
程序的输出是什么!? 我的分段错误...这是什么?
#include<stdio.h>
void f(void)
{
int s = 0;
s++;
if(s == 10)
return;
f();
printf("%d ", s);
}
int main(void)
{
f();
}
what is the output of the programme!??
i m segmentation fault ...what is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
由于
s
是局部变量,因此对f()
的每次递归调用都会获取它自己的副本。因此,每次s
都会为 1,并且您会收到堆栈溢出异常。Since
s
is a local variable, each recursive call tof()
gets its own copy of it. So every times
will be 1 and the you get a stack overflow exception.这里的问题是您没有静态初始化
s
。因此,每次调用f()
时,它都会始终从 0 开始,并且f()
不断地一遍又一遍地调用自身,但是if(s == 10)
永远不会满足。调用堆栈最终会溢出并出现分段错误。The problem here is that you're not statically initializing
s
. So it'll always start off as 0 every time you callf()
, andf()
keeps calling itself over and over again butif(s == 10)
is never met. The call stack eventually overflows and boom, segmentation fault.问题是您递归地调用
f
,而每次都没有更改任何内容。您可能会遇到堆栈溢出。每次调用
f
时,都会将s
初始化为0
并递增它。它永远不会达到 10。您应该做的是将
s
作为f
的参数,或者使s
静态。The problem is that you're calling
f
recursively, without changing anything each time. You're probably getting a stack overflow.Each time you call
f
, you initializes
to0
and increment it. It will never reach 10.What you should do is either make
s
a parameter tof
or else makes
static.你的程序是一个无限循环;你会得到分段错误,因为你最终会溢出堆栈。
Your program is an infinite loop; you get the segmentation fault because you eventually overflow your stack.
程序进入无限循环。
原因是:
s 是 f() 的局部变量,因此每当调用 f() 时,s 都会被初始化为 0。然后它会加 1,& f() 再次被调用。这次,f() 也使用其本地副本 &将 s 初始化为 0 &再次递归调用 f()。这会生成一个无限循环 &程序产生分段错误。 seg错误的原因是由于递归无限调用而导致堆栈溢出。
要解决这个问题,要么将 s 设置为静态,要么将其设置为全局的,而不是本地的。
The program goes into an infinite loop.
The reason for that is :
s is local to f(), so whenever f() is called, s is initialized to 0. Then it is incremented by 1, & f() is again called. This time as well, f() uses its local copy & initializes s to 0 & again calls f() recursively. This generates an infinite loop & the program generates a segmentation fault. The reason for seg fault is stack overflow due to recursive infinite calls.
To remedy this, either make s static or make it global, rather than local.
让你的变量成为静态怎么样?
How about making your variable s static?
这是一个调用自身的递归函数。然而,这是一个无限循环,因为
int s = 0;
每次调用时都会重置计数器。它将打印出无穷无尽的 0 行。示例解决方案:
我不擅长 C 语法,但这个想法是合理的。
This is a recursive function that calls itself. However, it is an infinite loop because
int s = 0;
resets the counter every time it is called. It will print an endless line of 0s.Sample solution:
I'm not good with C syntax, but the idea is sound.
无限循环。这可能与您想要的类似
Infinite loop. Here is probably something similar to what you want
这可能是一个更有趣的程序:
它将打印:
9 8 7 6 5 4 3 2 1
This might be a more interesting program:
That will print:
9 8 7 6 5 4 3 2 1
该程序的输出是该站点的名称。
正如其他人建议的那样,您应该将
s
声明为static int s = 0
The output of this program is the name of this site.
As others suggested you should declare
s
asstatic int s = 0