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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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?
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
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?
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
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...
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
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.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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..
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
But the question was already asked and answered, so why are you asking it again?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953