Why do we need to use `int main` and not `void main` in C++?
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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) andEXIT_FAILURE
. The meaning of other possible return values is implementation-defined.
Steve P.
Updated on July 09, 2022Comments
-
Steve P. almost 2 years
Why do we need to use
int main
and notvoid main
in C++? -
Lazy over 15 yearsAnd it still is helpful. Commandline scripts are not dead, you know? ;-)
-
Powerlord over 15 yearsThis is a bit late, but I thought the C standard only defined int main() and int main(int argc, char *argv[]).
-
Keith Thompson over 11 yearsThe C standard permits
main
to be defined asint main(void)
, asint main(int argc, char *argv[])
, or equivalent, or "or in some other implementation-defined manner". The C++ standard's requirements are similar, except thatmain()
must always be defined to returnint
. (This applies only to hosted environments; freestanding (i.e., embedded) systems can define the program's entry point any way they like.) -
Keith Thompson over 11 yearsThe "short" answer is really the only answer. It would have made perfectly good sense to permit
void main() { ... }
; you could still useexit()
to return an exit code to the environment, or just default to0
if you reach the end ofmain
. The reason not to usevoid main()
is that the language standard doesn't permit it, any more than it permitsdouble main(long long foo, time_t bar)
. -
saurabh gupta over 5 yearsI like your explanation. Could you please provide a working example [a script that actually works]
-
WISAM about 5 yearsthanks..good explanation