B.Gen.Jack.O.Neill
B.Gen.Jack.O.Neill

Reputation: 8397

C++ void return type of main()

Some C++ compilers allow the main function to have return type void. But doesn't the Operating System require int type value returned to specify whether the program ended well or not?

Upvotes: 14

Views: 11970

Answers (8)

Manoj R
Manoj R

Reputation: 3247

But doesn't OS require int type value returned to specify whether 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.

Upvotes: -1

JoeG
JoeG

Reputation: 13182

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

Upvotes: 3

user372743
user372743

Reputation:

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.

Upvotes: 0

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

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.

Upvotes: 20

heb
heb

Reputation: 753

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().

Upvotes: 1

Cătălin Pitiș
Cătălin Pitiș

Reputation: 14341

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.

Upvotes: 5

Rob Kennedy
Rob Kennedy

Reputation: 163277

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.

Upvotes: 30

Etienne de Martel
Etienne de Martel

Reputation: 36852

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

Upvotes: 4

Related Questions