Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why use void main () instead of int main(void)?

Status
Not open for further replies.
Nov 1, 2003
1
US
What is the advantage to using void in void main ()
instead of using say; int main (void) or float main (void)
or double main (void)?

I understand that void is not needed in the parenthesis since it is assumed by the compiler, but why would you use

one to start other than that your program may be using alot of integers, floating, or double precision but just

how do you know which one to use when or if it's even neccessary to use them?

can you just say: Main (void) and be done with it that way?

 
The reason that main is defined as

int main( int argc, char* argv[], char* env[] )

is to enable the program to communicate with the operating system when the program terminates.

For a normal program termination, the return value is 0 (zero).

i.e.
int main ()
{
return 0; //this returns the value 0 to the operating system.
}

If an error has occurred in the execution of a program, then an error number can be returned.

i.e.
int main()
{
return 1; // this returns the integer value 1 to the operating system
}
or
int main()
{
exit ( 1 ); //this function closes all open files and returns the integer value 1 to the operating system
}

In many operating systems it is possible to determine the value returned by a program when it terminates in order to detect errors.

Defining main() as anything other than int will produce a compile time error or warning.

Float, Double etc. are generally a bad idea.

Hope this helps
Zeit.

 
main is a function like any other function in C++. A function can return a value, or it can return void (nothing). A function can also take parameters.

main is a special function, though, so for the reasons zeitghost gave it should return int. Some compilers allow void main (like Microsoft Visual C++), but that is not "correct" C++ or C. See this FAQ from Bjarne Stroustrup (the creator of C++) for more on why void is wrong:

 
I hate to disagree with a major figure like Bjarne Stroustrup, but I've always felt this an inconsistent feature of C. It seems to me that any C-like compiler should be entitled to return a type from its main function that suits the operating system for which it is intended. The return type should not have been fixed in the syntax of the language.

In that way, if you are lucky enough to have an operating system that provides more information about what went wrong than a single integer, you can use that feature. As it is, you can't. Not only is it not standard (and lets face it, anything that depends on the OS cannot be standardised totally), it is Forbidden and Wrong.
 
Current ISO/ANSI C++ Standard, 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:
Code:
  int main() { /* ... */ }
and
Code:
  int main(int argc, char* argv[]) { /* ... */ }
And from 3.6.1/5:
... If control reaches the end of main without
encountering a return statement, the effect is
that of executing
Code:
  return 0;
Have you noticed that implementation-defined
but not user-defined (about so called freestanding i.e. dedicated implementations)?
Any real word language standard must include some assumptions about its execution environment.
What's a question? C++ Standard has more interesting weaknesses or hard points (they are innumerable;)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top