Why can C main function be coded with or without parameters?

前端 未结 5 721
离开以前
离开以前 2020-12-06 12:14

Just wondering why this

int main(void){}

compiles and links

and so does this:

int main(int argc, char **argv){}
         


        
5条回答
  •  情话喂你
    2020-12-06 12:36

    Because the calling code can, for example, pass arguments in registers or on the stack. The two argument main uses them, while the zero argument main does nothing with them. It's that simple. Linking does not even enter the picture.

    If you are worried about stack adjustments in the called code, the main function just needs to make sure the stack pointer is the same when it returns (and often even this is of no importance, e.g. when the ABI states that the caller is responsible for stack management).

提交回复
热议问题