阅读下面的程序并告诉我输出是什么。
f(int n)
{
static int i=1;
if(n>=5) return n;
n=n+i;
i++;
}
main()
{
f(1);
}
这本书CS Computer Science and IT: Practice Book for PSUs我收到的问题是 7
,但在编译时出现“退出失败”
这可能是什么原因?谁能给我解释一下这个吗?
<小时/>代码来自comments至 Greg Hewgill的answer :
#include <stdio.h>
#include <string.h>
static int i=1;
f(int n)
{
if(n>=5)
return n;
n=n+i;
printf("Before - %d\n",i);
i++;
printf("after - %d\n",i);
}
main()
{
int y;
y=f(1);
printf("Main - %d\n",y);
return 0;
}
最佳答案
您的 main()
函数没有返回任何内容(这意味着它的返回值未定义,并且 main()
的返回值被报告为退出代码该程序)。将其更改为:
int main()
{
f(1);
return 0;
}
现在,如果您希望程序打印任何内容,那么您必须添加语句来执行此操作。
关于c - 为什么以下程序退出失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341148/