主函数的返回值返回给谁,'main'函数的返回值在哪里去?

In C, a function always returns its value to the calling function and never to itself (if return type is not void). Like,

int main()

But since 'main' function is called by the Operating System, which is not a function. So, whom does the 'main' function returns its value? Where does the value go, when its returned using the expression

return(0);

at the end of the program?

解决方案

As already said, this is OS specific. In UNIX systems it looks something like this:

When you compile a program with gcc, it wraps a startup routine around your main() function. This routine calls your main() function and saves its return value. It then calls the exit() function (which your program might call as well), that does some general clean up. This function then again calls _exit(), which is a system call that tells the OS to save the returned value of your main() function in the process table (where meta information about your process is saved). As soon as another process calls wait() on your process id (PID), your returned value is given to the calling process and your process is removed from the table.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值