Search code examples
c++ccompilationprogram-entry-point

Is it possible to write a program without using main() function?


I keep getting this question asked in interviews:

Write a program without using main() function.

One of my friends showed me some code using macros, but I could not understand it.

So the question is:

Is it really possible to write and compile a program without main()?


Solution

  • Within standard C++ a main function is required, so the question does not make sense for standard C++.

    Outside of standard C++ you can for example write a Windows specific program and use one of Microsoft's custom startup functions (wMain, winMain, wWinmain). In Windows you can also write the program as a DLL and use rundll32 to run it.

    Apart from that you can make your own little runtime library. At one time that was a common sport.

    Finally, you can get clever and retort that according to the standard's ODR rule main isn't "used", so any program qualifies. Bah! Although unless the interviewers have unusual good sense of humor (and they wouldn't have asked the question if they had) they'll not think that that's a good answer.