gpt4 book ai didi

c++ - 使用 exit(1) 从函数返回

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:45 25 4
gpt4 key购买 nike

Linux gcc 4.4.1 C99

我只是想知道使用以下技术是否有任何优势。我注意到我正在阅读一些代码,导出编号的值上升了,如该代码片段所示。

/* This would happen in 1 function */
if(test condition 1)
{
/* something went wrong */
exit(1);
}

if(test condition 2)
{
/* something went wrong with another condition*/
exit(2);
}

或执行以下操作并返回:

/* This would happen in 1 function */
if(test condition 1)
{
/* something went wrong */
return;
}

if(test condition 2)
{
/* something went wrong with another condition*/
return;
}

最佳答案

exit() 退出你的整个程序,并报告你传递给它的参数。这允许任何正在运行您的程序的程序找出它错误退出的原因。 (1 可能表示无法连接到数据库,2 可能表示意外参数等)。

Return 只返回你所在的当前函数,而不是整个程序。

关于c++ - 使用 exit(1) 从函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972953/

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