如果main为空,D如何在成功时返回0而在失败时返回非零? [英] How can D return 0 on success and non-zero on failure if main is void?
问题描述
在D中,定义了 main
函数:
void main(/*perhaps some args but I do not remember*/)
{
}
我确定该函数成功返回零,失败返回非零,但它被定义为不返回任何东西。背后的逻辑是什么?
I know for sure that this function returns zero on success and non-zero on failure and yet it is defined as not returning anything. What is the logic behind it?
推荐答案
具有void返回类型的函数不会返回任何值。当您认为调用堆栈看起来像这样时,没有什么不合逻辑的:
function with void return type does not return any value. There is nothing illogical when you consider call stack looks like this:
OS -> D runtime -> main
D运行时系统调用main函数,该运行时系统认识到main函数不返回任何内容-并且在这种情况下,将成功返回操作系统。如果主函数定义为返回类型int,则D运行时返回主函数返回的OS值。
The main function is invoked by D runtime system, which recognized that the main function returns nothing - and in this case return success to the OS. In case the main function is defined with return type int, then the D runtime return to OS value returned by main function.
这篇关于如果main为空,D如何在成功时返回0而在失败时返回非零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!