int main()

May 14, 2012 at 5:25am
As you might notice, I am a new user. I apologize if this is the wrong forum and/or if this question sounds silly. Well, my question is about main(). In every C++ program I have seen, int main() is mandatory over main() or void main(). Is there are reason why it has to be int main all the time, or am I just missing something really obvious. Shouldn't it be allowed to let us give other types as well?
May 14, 2012 at 5:46am
The C++ standards document section 3.6.1 - says that main should return int.
End of discussion on that point.
May 14, 2012 at 10:08am
To add to guestgulkan (who is absolutely correct - the C++ definition gives no other option - main returns an int and that's all there is in C++), the function that calls main expects main to return an int and will behave accordingly. Screwing with it is just silly.
May 14, 2012 at 1:20pm
One more thing I (another person) was wondering. Is there any reason those who made the language decided to make main() return int instead of void or something else of the sort? (A void main() in the standards would be more intuitive.)
May 14, 2012 at 1:28pm
closed account (1vRz3TCk)
The int is a return code for the OS. It tells the OS the reason for the program terminating.

Edit:

See: http://en.wikipedia.org/wiki/Exit_status
Last edited on May 14, 2012 at 1:32pm
May 14, 2012 at 1:28pm
Returning a value gives a way for the exit state (i.e. success/error description) to be communicated back to the caller of main.
May 14, 2012 at 1:29pm
A void main() in the standards would be more intuitive
¿why?
If you can't return anything, it will be a little hard to do scripting.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms680582%28v=vs.85%29.aspx I suppose that this is official...
May 14, 2012 at 2:14pm
closed account (S6k9GNh0)
In the D language, they allow a "void main()". This is because most users do not care to return anything in main since they never use it. But in reality, main still returns an "int", the user just doesn't do anything with it and the return value is junk. In other words, it's still not ethical to use "void main()" in D even though it's allowed.

If C/++ were to allow such a thing, most likely the same thing would happen because of the nature of things. You can only define one type of "main".
Last edited on May 16, 2012 at 9:28pm
Topic archived. No new replies allowed.