c - 为什么以下程序退出失败?

标签 c compiler-errors

阅读下面的程序并告诉我输出是什么。

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,但在编译时出现“退出失败

这可能是什么原因?谁能给我解释一下这个吗?

<小时/>

代码来自commentsGreg Hewgillanswer :

#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/

相关文章:

地穴(): Pointer to integer without cast?

c - 如何获得线性方程的系数

java - 如何设置 JButton 的按下背景颜色?

c++ - Visual Studio 2015 中的预编译 header 错误

c - 新节点损坏。无法弄清楚出了什么问题?

c - FFmpeg 不支持不同容器的比特率?

c - 数组如何在结构中工作?

delphi - 不满意的转发或外部声明

c++ - 使用 Info-ZIP 3.0 源代码时出现编译器错误

typescript - 使用[key : string]时, typescript 编译错误