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

前端 未结 4 707

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:45

    This is completely OS-specific, but usually the OS invokes a program by

    1. Setting up the program's address space,
    2. Creating a record of the new process somewhere in the OS internals,
    3. Launching its own custom function, which calls main and stores the return value in the OS internals.

    The function I'm referring to in (3) is often a good old fashioned C function that just jumps into main so it can capture the return value from main as if it were an ordinary C function (which it in fact is). Afterwards, it stores that value somewhere in OS-land so that processes that need to read the return value can do so.

    Hope this helps!

提交回复
热议问题