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

前端 未结 5 716
离开以前
离开以前 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:58

    The short answer: if you don't use the parameters, then you can declare main without parameters, in two ways:

    int main(void)
    

    or

    int main()
    

    The first means main is a function with no parameters. The second means main is a function with any number of parameters.

    Since you don't access the parameters, both will be fine. Any compiler having "special" code to check the parameters of main is wrong. (But: main must return a value.)

提交回复
热议问题