tags:

views:

832

answers:

8

why do we need to use int main and not void main in c++?

+13  A: 

The short answer, is because the C++ standard requires main() to return int.

A long answer can be found at void main() is not legal in C++ but is legal in C.

As you probably know, the return value from the main() function is used by the runtime library as the exit code for the process. Both Unix and Win32 support the concept of a (small) integer returned from a process after it has finished. Returning a value from main() provides one way for the programmer to specify this value.

Greg Hewgill
This is a bit late, but I thought the C standard only defined int main() and int main(int argc, char *argv[]).
R. Bemrose
+10  A: 

Most Operating Systems report back to the user, or the calling process, if an application was successful or not. This is especially useful in scripting, where the script can conditionally branch (if-then) on the results of a program. Something along the lines of:

// pseudo-code
screenscrape  http://mydatasource.com > results.txt
if errorlevel == 0 then
   processfile results.txt
else
   echo Screen Scraping Failed!
end if

This result status is done via the return value of main.

While some compilers allow for void main, for the sake of consistency and simplicity, the ANSI standard requires one single prototype of main:

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

Because in C, arguments are cleaned up by the caller, the author of main can neglect to declare or process the arguments argc & argv. However, if the setup-routines that call main expect an int return value, and instead don't find one, behavior can undefined.

Short answer:

  • The return value of main is useful for scripting.
  • The setup and cleanup routines that invoke main need a consistent interface to use.

P.S. I touch on this on a C-brain teasers page I'm writing. Its still under construction, but this article may be worth a read: http://abelenky.com/node/3

abelenky
+1  A: 

Please check this article at C++ FAQ Lite, and this article at FAQ Cprogramming.

Igor Oks
A: 

As in C, because the process will give the OS an exit code.

You can either use

int main (int argc, char ** argv)
{
  return (0);
}

or

int main (int argc, char ** argv)
{
  exit (0);
}

This is at least in C89 IIRC.

Keltia
A: 

Because int is the returncode a program can return to the OS.

You can query this value to check if operation has been succesfull.

This was extremely helpfull when using commandline scripts.

Gamecat
And it still is helpful. Commandline scripts are not dead, you know? ;-)
peSHIr
A: 

From Wikipedia:

The value returned from the main function becomes the exit status of the process, though the C standard only ascribes specific meaning to two values: EXIT_SUCCESS (traditionally zero) and EXIT_FAILURE. The meaning of other possible return values is implementation-defined.

BBetances
A: 

Perhaps because it makes sense to cleanly exit with a status code from the main() method. In java, we have to emulate this using System.exit() which is not all that graceful.

Rahul
A: 

You can check out int main() vs void main() for full details on this question.