相关疑难解决方法(0)

为什么没有编译器错误的main()没有返回结束?

我正在研究C-brain预告片:编写标准的Hello-World程序,没有分号.

到目前为止我的最佳答案是:

int main(void)
{
    if (printf("Hello World!\n"), exit(0), 0)
    {
        /* do nothing */
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么我没有得到编译器错误(Visual Studio):

error C4716: 'main' : must return a value
Run Code Online (Sandbox Code Playgroud)

我已尝试使用声明的返回类型的其他函数,但缺少return语句,并得到此编译器错误.


请注意,我也尝试过:

int foo(void)
{
    if (printf("Hello World!\n"), exit(0), true)
    {
        /* do nothing */
    }
}

int main(void)
{
    foo();
}
Run Code Online (Sandbox Code Playgroud)

并且不要在foo上得到编译器错误.如果我删除"退出(0)",我确实得到编译器错误.显然编译器知道"退出"是一个特殊的功能?这对我来说似乎很奇怪.

c compiler-errors

9
推荐指数
2
解决办法
2325
查看次数

标签 统计

c ×1

compiler-errors ×1