i hope that this does not sound like a silly question but can someone please tell me why we return an integer from the main method in c++?

if program execution starts and ends in main then what is the point? main calls other mothods and receive return types so why does main have to return something itself?

Historically,
- C++ was derived from C.
- In C, everything was an int unless specified otherwise (there was no void), so it was natural for main to return int.
- C programs were originally developed for command line use, and using a command line interpreter which was far more capable 30 years ago than M$ cmd.exe is even today. That command interpreter could easily determine whether the program just ran was a success (or not) from the value returned by main.

I always learned that main returns an int so that you can put exit(0) , exit(1) and other constructions in to tell you why the program quit. Or would that not work...?

yup. The code is returned to the operating system and can be polled from other programs (like shellscripts) to determine the reason why the program terminated (with 0 traditionally indicating that everything worked smoothly).

I always learned that main returns an int so that you can put exit(0) , exit(1) and other constructions in to tell you why the program quit. Or would that not work...?

Interesting question, to rephrase:
If i have a void main() and in the code I write exit(11). When I call this program from command line what will be return code of it?

Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)

char* main()
{
   char* ptr;
   return ptr;
}

But since pointers are really integers the program will actually return the integer value of that pointer. Meaningless -- absolutely. But I've seen someone try to do it.

if program execution starts and ends in main then what is the point? main calls other mothods and receive return types so why does main have to return something itself?

Like others have said, main's return value is used to determine if the program ran successfully or what the errors were. As to why the mechanism is a return value, when your program runs, something in your OS is calling main. When your program finishes, main returns an int to the OS function which called it.

Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)

char* main()
{
   char* ptr;
   return ptr;
}

I tried it on VC5 and it failed?

So, VC5 isn't one of them. Although I think the standard says an int-compatible type or somesuch, so a pointer would fit that...

You're still wrong even if your only way out is via exit()
http://c-faq.com/ansi/voidmain3.html

Sure, doing a void main() is wrong. But at least if I don't have control over main() and still use exit(123) can I expect 123 as the return code of the executable on command line? Assume we're using Unix and gcc.

Did you even bother to read the link before posting?

may not even be able to call it correctly

If main() cannot be called by the run-time, then your use of exit() as your way out is a moot point. The program is dead before it began.

Did you even bother to read the link before posting?

Yes I did. Come on man, after having some nice time fighting with you, I have to be mad not to.. :)
The question can still remain..

But the question was already asked and answered, so why are you asking it again?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.