Compiling with int main(void) fails; main(int argc, char *argv[]) succeeds. Why?

后端 未结 2 540
清歌不尽
清歌不尽 2020-12-19 11:22

Problem

Why would compiling a program which has an int main(void) main function differ from compiling a program which has an int main(int argc,

相关标签:
2条回答
  • This is SDL thing. On Windows, when you include SDL.h,main is redefined to SDL_main which calls WinMain (the real entry point in non-console Windows apps), does some initialization and finally calls your main code. It has a signature with argc and argv and you're pretty much required to follow it, so int main() won't work.

    0 讨论(0)
  • 2020-12-19 11:57

    The specification of main(...) is a contract. In the C language, the contract says that the arguments are int and char **. This is a requirement your program has to fulfill, if it wants the environment to interact with it.

    Whether or not your program wants to use the parameters is a different issue -- it just has to abide by the contract that there is a functiona named main, with the correct order and type of parameters.

    0 讨论(0)
提交回复
热议问题