Is the "main()" function REQUIRED these days?

While it may seem a foolish question, I'm trying to get caught up with some of the things added and done with the C++ language since I've been away.

I ask this question, as I'm curious. Something that bit me about 20 years ago using Borland's C++ compiler (don't recall version). I was following some book along that had some interesting code in it, and as most programmers "learn by doing", I simply compiled the source, and tested the code "as it was".

Then, attempting to make changes to the code, I searched for the main() function so I could trace the code, make some slight changes and re-run to make sure I had down what the author was doing. However, there was no main() function anywhere. It wasn't overridden, or played with using a #define, etc.

The program actually started by declaring a global variable from a class, and the constructor for the class is what actually "started" the code. I HAVEN'T mis-remembered this - it took me 3-4 days to track down and run in debug, etc. to determine just what was going on. I thought it was the stupidist thing I've ever run into - but that was the way the code was written.

So - I'm just trying to find out if main() is still not required (or is this a vendor implementation thing), or the standard has changed, etc. and main IS now required. Did some searching and couldn't find a definitive answer.
The main function is definitely still required in C++11.
http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B
I like to declare/define it as
1
2
3
signed main(unsigned nargs, char const *const *args)
{
}
but that's just my personal preference.


In some compilers, it may be WinMain or _tmain, simply because the company that wrote the compiler was determined to lock you to their platform.
Last edited on
it may be WinMain or _tmain, simply because the company that wrote the compiler was determined to lock you to their platform
It's because Windows applications need to get passed special parameters from the windowing system that main() doesn't support.

main() is only required when you're writing a C/++ program. If you're writing a library main() is not required, since libraries don't necessarily have an entry point.
Thanks for the reply.

And no - no main was suppled at the time by the compiler (at least none that I could determine through the use of debug, etc.). The code itself was not windows specific, and I believe I used DOS or Console project for the entire project (Console might have provided something - but DOS wouldn't have).

It was some database-type code, and the book and code were NOT compiler specific either (at least none were mentioned on the book). I didn't try it with other compilers at the time, just Borland. So I don't think it was some compiler specific quirk - as the code was supposed to work in "any" compiler at the time. As I said, this was about '93-94 timeframe.

Thanks for the replies
helios wrote:
It's because Windows applications need to get passed special parameters from the windowing system that main() doesn't support.
OK, so not nearly as bad as I thought. Thanks - I didn't know because I don't use the Windows API (it scares me).
Technically, C++ supports freestanding compilation model (OS kernels, bootloaders, etc), which don't require the main() function. Standardese: "It is implementation-defined whether a program in a freestanding environment is required to define a main function." ยง3.6.1[basic.start.main]/1

As for Windows, _tmain() is just a macro which can be ignored (unless you care about processing UTF-16 commandline arguments) - main() works too.
Last edited on
Topic archived. No new replies allowed.