tags:

views:

399

answers:

9

Hi, just a quick question. C++ allows main function to have return type void. But doesent OS require int type value returned to specify wheather program ended well or not? Thanks.

+4  A: 

The C++ standard does not allow main() to have a return type of void. Most compilers will let it pass for historical reasons, though.

Etienne de Martel
+21  A: 

C++ does not allow main to have a void return type. The published C++ standard requires it to be int. Some C++ compilers allow you to use void, but that's not recommended. In general, the OS doesn't care one way or the other. A specific OS might require a program to give a return value, but it doesn't necessarily have to come from main's return value. If the C++ compiler allows void, then it probably provides some other means of specifying the program's exit code.

Rob Kennedy
+5  A: 

main returning void is accepted for backwards compatibility, but it is not legal.

In this case, the exit code will be 0. You can still change the exit code, using exit function.

Cătălin Pitiș
+16  A: 

C++ allows main function to have return type void

No, it doesn't.

The C++ standard only requires 2 different types of main signatures. Others may be optionally added if the return type is int.

Implementations of C++ which allow void return types are incorrect in terms of the C++ standard.

C++03 standard S. 3.6.1-2:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ } 
int main(int argc, char* argv[]) {/* ... */ }

If you want portable C++ code, or to write good C++ examples then you should always use one of the 2 variations above.

Brian R. Bondy
+1  A: 

That's why void main() is not allowed by standard C++ - though some compilers (e.g. gcc) does allow it.

To make it short: always use int main(), never void main().

heb
gcc allows it for C but forbid it for C++, even if you tell it to be -fpermissive.
BatchyX
+1  A: 

In languages where a void return from main is legal (i.e. not C++), the OS usually sees a return value of 0 on normal (non-exceptional) program termination.

Joe Gauterin
A: 

as we know the program execution starts from main but main() is never called in the program Actually it is called by operating system

http://techlaugh.com

anupam
A: 
But doesent OS require int type value returned to specify wheather program ended well or not?

Why would it always? On windows when you double click on the icon, the process dies after it ends. OS do not check for the return type there. Even on linux if you just run the binary as ./runBinary, it simply runs and exits. The OS do not show message by itself that it fails or succeeds.

All the above answers are right that the standard says it is int, but some compilers allow void too.

Manoj R
A: 

Depending on the compiler, you may be able to use a void main function, however the proper way (that a truly standard compliant compiler should follow) is to return int with 0 being a nice & clean exit and anything else indicating that your program has done something wrong.

Glenn Nelson