Void main and Int main

closed account (LN7oGNh0)
OK. I have heard that using void main is like murder, but I cant find exactly why.
Also, if it is such a bad thing, why do I see some people using it???
Lastly, if some people are using it, then what effect does it have on the code?

Thanks!
I have never seen anyone use it. Main is supposed to return 0 to the calling program (the OS) if it ended successfully, and anything else if it didn't. How can it return anything if it's void?
closed account (LN7oGNh0)
Alright, but i see some codes, which include void main. And if people are using it and it cannot return 0 to end the program, then how is it possible to use it in C++?
From the daddy of C++:
http://www.stroustrup.com/bs_faq2.html#void-main

By the way, did you know that you needn't actually return anything from main()?
1
2
3
int main()
{
}
closed account (LN7oGNh0)
Is that only when there is no code?
closed account (LN7oGNh0)
So Stroustrup said that it is not part of C++, then why would anyone put it into a c++ compiler? (Microsooft Visual C++ 2010)
Is that only when there is no code?

No. If control reaches the end of main, it's as if you had a return 0; there (however it's important to remember that this only works for main, not for other functions).

So Stroustrup said that it is not part of C++, then why would anyone put it into a c++ compiler? (Microsooft Visual C++ 2010)

C++ existed long before it was standardized in 1998 and basically each compiler vendor did what they wanted to. There were variants that accepted void main() and in order not to break existing code (probably), Microsoft decided to still allow it after 1998 (GCC used to accept void main() as well until a few years ago).
Last edited on
Is that only when there is no code?

No, and that link also mentions this exception and gives a "full" example.

So Stroustrup said that it is not part of C++, then why would anyone put it into a c++ compiler?

Why not? It's their compiler and they can add any features they like.
For instance many C compilers nowadays support the __typeof__ extension. Which isn't, and may never become part of the C standard.

http://publib.boulder.ibm.com/infocenter/comphelp/v101v121/topic/com.ibm.xlcpp101.aix.doc/language_ref/typeof_operator.html
closed account (LN7oGNh0)
OK! Thanks everyone!!!
Topic archived. No new replies allowed.