41

Why do we need to use int main and not void main in C++?

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
kasperasky
  • 2,869
  • 5
  • 19
  • 16

8 Answers8

39

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

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.

jmq
  • 9,602
  • 14
  • 54
  • 68
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • This is a bit late, but I thought the C standard only defined int main() and int main(int argc, char *argv[]). – Powerlord Feb 06 '09 at 21:40
  • The C standard permits `main` to be defined as `int main(void)`, as `int main(int argc, char *argv[])`, or equivalent, or *"or in some other implementation-defined manner"*. The C++ standard's requirements are similar, except that `main()` must *always* be defined to return `int`. (This applies only to hosted environments; freestanding (i.e., embedded) systems can define the program's entry point any way they like.) – Keith Thompson Sep 24 '12 at 20:56
  • 3
    The "short" answer is really the *only* answer. It would have made perfectly good sense to permit `void main() { ... }`; you could still use `exit()` to return an exit code to the environment, or just default to `0` if you reach the end of `main`. The reason not to use `void main()` is that the language standard doesn't permit it, any more than it permits `double main(long long foo, time_t bar)`. – Keith Thompson Sep 24 '12 at 20:58
16

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.
jmq
  • 9,602
  • 14
  • 54
  • 68
abelenky
  • 58,532
  • 22
  • 99
  • 149
6

Main reason for changing

void main() { }

to

int main() { }

in later releases was to notify error occurred in program during execution to operating system on which it running

return 0;

identify program successfully executed if any number rather then 0 returned that means some error occurred who's error code is which returned by main. if you are running on codeblock IDE see in build log if main return 0 it normally display

Process terminated with status 0

else it display status code in red which means an error occurred

Code Warrior
  • 408
  • 6
  • 14
1

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.

Azeem
  • 7,094
  • 4
  • 19
  • 32
BBetances
  • 925
  • 1
  • 14
  • 23
0

When we execute our program to check it runs successfully or not. So when it returns 0 that means it's true & ran successfully, if it returns 1 then it's not run successfully & this int value tells the OS if the program ran successfully or not

Robert
  • 5,191
  • 43
  • 59
  • 113
0

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
  • 13,953
  • 3
  • 27
  • 30
0

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.

Toon Krijthe
  • 50,865
  • 37
  • 137
  • 200
0

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.

Azeem
  • 7,094
  • 4
  • 19
  • 32
Rahul
  • 12,392
  • 13
  • 55
  • 59