Where does the returned value for 'main' function go?

前端 未结 4 626

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

i         


        
4条回答
  •  广开言路
    2021-01-15 02:58

    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

提交回复
热议问题