gpt4 book ai didi

c++ - 没有明确使用返回/退出的任何退出状态

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:50 25 4
gpt4 key购买 nike

这实际上困扰了我一段时间。问题是这样的:How to set the exit status of a program to any value without explicitly using return/exit in gcc/g++ ?让我们考虑这段代码:(从 stdin 获取输入并打印它到标准输出,直到遇到零输入 0)

#include <stdio.h>

int main() {
int n;
while(scanf("%d",&n) && n>0 )
printf("%d\n",n);
}

在我的系统(windows + mingw)中它返回 1,How to make它隐式返回 0 或其他任何内容而不显式使用退出/返回?

编辑:

我稍微修改了一下代码:

int f(int n) { 
return (n>0);
}

int main(){
int n;
while(scanf("%d",&n)&&f(n))
printf("%d\n",n);
}

它现在隐式返回 0,但我无法从中得出任何确定的结论。

最佳答案

在 C++ 和 C99 中,离开 main 而不执行 exit() 或 return 应该等同于 return 0(C 参见 5.1.2.2.3,C++ 参见 3.6.1/5),如果我记得的话,C89 未定义正确。

关于c++ - 没有明确使用返回/退出的任何退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com