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/