'main'函数的返回值在哪里去? [英] Where does the returned value for 'main' function go?

查看:646
本文介绍了'main'函数的返回值在哪里去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,一个函数 always 将其值返回给调用函数,并且永远不会自动返回(如果返回类型不是void )。就像

  int main()

但是由于 main '功能是由操作系统调用的,不是功能。那么,'main'函数返回它的值呢?当值使用表达式返回时,值的位置在哪里?

  return(0); 

在节目结束时?

解决方案

如前所述,这是特定于操作系统的。在UNIX系统中,它看起来像这样:



当你使用gcc编译一个程序时,它会围绕你的 main()函数。这个例程调用你的 main()函数并保存它的返回值。然后调用 exit()函数(您的程序可能会调用该函数),这会进行一些常规清理。这个函数再次调用 _exit(),这是一个系统调用,它告诉操作系统保存 main()在进程表中的功能(保存你的进程的元信息)。只要另一个进程在你的进程ID(PID)上调用 wait(),你的返回值就会被赋给调用进程,并且你的进程将从表中移除。 p>

查看此资源获取更多信息: http://www.johnloomis.org/ece537/notes/Processes/process_environment.html


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.

Look at this resource for more information: http://www.johnloomis.org/ece537/notes/Processes/process_environment.html

这篇关于'main'函数的返回值在哪里去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆