我目前正在学习 c,我对返回感到困惑:
int factorial (int n)
{
if (n == 1)
return 1;
else
return n * factorial (n -1);
}
上面的递归代码中最后一次调用当n为1时,return 1会返回整数值1还是会像main中那样执行时出错。我有这种困惑,因为 return 1 在 main 和 main 调用的函数中的处理方式不同......
最佳答案
Will the
return 1
return the integer value one or error in execution like in main?
return 1
从任何声明为返回 int
的函数返回整数值 1
,甚至如果该函数恰好是 int main()
。
来自 main()
的非零返回值被解释(由执行环境,例如 shell)以指示执行中的错误。这恰好是 main()
返回值的语义。
关于c - 非主函数中返回1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40481842/