void main() or int main()?

Pages: 12
Hi! I'm in doubt when should I use it (void main or int main)and whats difference between them?

If i write program using void main() what could happen?

#include <iostream>
using namespace std;

void main() // use otherwise int main, void main.
{
int y = 5;

for(int x = 0; x <= y; x++)
cout << x ;

system("pause")
return 0;


just use
int main()
{
}

for windows - it is "Good code style".
When? Always use int main. Chances are that if you have to ask, it won't matter, but it's good practice.
Not only is it good practice, it's also the standard.

http://www.parashift.com/c%2B%2B-faq-lite/newbie.html#faq-29.3
Another helpful link with an example of how "void main" can fail even if your compiler permits it for whatever reason: http://users.aber.ac.uk/auj/voidmain.shtml
Last edited on
Well, you can argue about that. If you use something like
1
2
3
4
5
int main
{
  //Balh blah blah
  return true;
}


So, what the code above says, if your program was completed successfully , some true value for your OS. But here is the thing -OS does not care what do you return, -1, 0 or 10000. It doesn't give a damn thing, so what is the point of returning something if it is not taking it seriously?
Another "why it should be void" is that some beginner programmers to OO or not only to OO, might get to their head that every function should return something, so it could know if it was successful or not - this far away from true. There are things called try and catch which deal with error detection. Also, function which return something are called "getSomething" while void functions are called do "something".
In the end of the day, u write void main only once per program. Why do you bother with that.
Personally, I will keep using void main as I see no proper reasons for using int main.
Ortonas wrote:
Personally, I will keep using void main

You'd limit yourself to the few C++ compilers that implement this non-standard language extension. Many compilers will refuse to compile your programs. Try gcc online: http://ideone.com/E75Ua

Last edited on
In C it's customary to return EXIT_SUCCESS or EXIT_FAILURE (or your own error codes), even if the OS itself doesn't care about it.

It only bugs me though, that main() is a special case and no error is issued for something like this:

1
2
3
4
int main()
{
    // no return
}


Edit: yes, I know return 0; is implicit for main().
Last edited on
I will keep using void main as I see no proper reasons for using int main.

What kind of skewed logic is that? There are no proper reasons for using void main, as int main() is the only allowed version ever since the first C++ standard in 1998. It's also one character shorter, for those who may care.
You can call programs from other programs. I think returning EXIT_SUCCESS (0) or EXIT_FAILURE can help to trigger some warnings in the parent executable.
Personally, I will keep using void main as I see no proper reasons for using int main.

translated
I'm going to keep using incorrect code for no good reason, and I'm never going to get a proper C++ compiler because it will stop me doing this, so my skills will get more and more out of date, but I just don't care.


You're the kind of coder we all pray we never have to work with. Fortunately, the kind of coder who does this sort of thing is exactly the kind of coder we will never have to work with.

OS does not care what do you return

No, your OS doesn't care. You have no idea what everyone else's OS does. Maybe some of us have proper grown-up operating systems instead of that toy you use. Maybe some of us code for platforms that don't even have an OS where the return value is of vital importance. Please, please understand that the world is bigger than YOUR chosen compiler running on YOUR chosen OS running on YOUR chosen hardware.
Last edited on
LOL! +1E99 @ Moschops. A bit over the top, but awesome nonetheless.

For all that care: (I speak about Windows only) The return value of an application is very important. For example, custom installers like BMC Software's allow custom post and pre-installation scripts that can be as simple as running some EXE tool. The software determines if the script was successful by examining the return code (zero is the standard conventional value for Success). And this technique is applied in many, many other places.

So as you can see, in the real world void main() has no place as must be shot down and burried deep.
Maybe this answer of the creator of the language will convince someone:
http://www2.research.att.com/~bs/bs_faq2.html#void-main
I don't get why there are so many people who think void main() is proper. It never has been and probably never will be standard C++, and in fact g++ will flat-out refuse to compile it even without -pedantic.

Although I suppose it is proper in C#. Maybe some of those people have a C# background?

-Albatross
Last edited on
closed account (S6k9GNh0)
Because new people think they know better than those who have used C++ for years which results in comments like the one Ortonas made.

Although, even veterans still do the same thing, often mispeaking outside of their own field of knowledge.
Last edited on
Well, if void main() is so standard and it must be shot down, so why then it is not deprecated?

You're the kind of coder we all pray we never have to work with.

Company will say what is standard what is not. How many per project you will write a function void main() ? Like...around... once?

Please, please understand that the world is bigger than YOUR chosen compiler running on YOUR chosen OS running on YOUR chosen hardware.

You should be working with green peace not as a coder. You know, they are all about saving animals, environment, they care about the world. Different OS, different compiler, different techniques, different needs, different standards. If I am working with the game, I do not give a damn thing how other people programed on-board computer for automobile.

You'd limit yourself to the few C++ compilers that implement this non-standard language extension. Many compilers will refuse to compile your programs. Try gcc online: http://ideone.com/E75Ua

Exactly, if not by the company, then by the compiler I will be told to use int main instead of void main. Big deal, one out of thousands compiler errors.

You can call programs from other programs. I think returning EXIT_SUCCESS (0) or EXIT_FAILURE can help to trigger some warnings in the parent executable.

If your program will hit some critical errors, it probably will be caught by try-catch blocks and the program will exit before the return of int main value.

Void main actually returns a value,but I am not sure what it exactly returns, but I am sure there is some logical in it.

Because new people think they know better than those who have used C++ for years which results in comments like the one Ortonas made.

You misinterpreting what I wrote. The moral of my story was "If you are not consider what program will return, then just leave it to compiler to deal with and use void main()."
Last edited on
closed account (S6k9GNh0)
I like how you avoided my comment.

If you read the link posted by eypros, you'll notice a comment saying that by using void main(), you're at risk of being called ignorant by regular C and C++ programmers. void main() isn't deprecated because it is not, nor was it ever, in the C++ standard. You cannot deprecate something that was never there in the first place. Thus it should not be used because it can 1)cause undefined behavior and 2) break compatibility with any standard abiding compiler. If you don't want to follow the standard, then you are not using C++ but some fucked up extension or alternative to C++.

You are ignorant.
Last edited on
Ortonas wrote:
Personally, I will keep using void main as I see no proper reasons for using int main.


To be honest there are no proper reasons to usevoid main() as it is not legal C++. Some compilers may allow it, but they are simply allowing you to put an error in your code without complaining.

Ortonas has a point... if the compiler allows void main() then it should silently add an implicit return 0; i.e. "deal with it".
If a superior in your company forces you to use any return type other than int for main in C++ even after you explain to them that it is not standards-compliant, then I think there's something rather wrong with the people you're working for. It's not even that large of a change and many compilers add a return 0; (albeit with a warning). In my mind, the benefits* of changing to int main() outweigh the costs many times over, don't they?

*Benefits include: Standards-compliance, greater compiler support, greater environment support, more options for reporting fatal faults within your program, no need to reroute your streams for error reporting if your program is called from another program (though there are only a few cases in which I think you should do that).

-Albatross
Last edited on
Pages: 12