Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to use `int main` and not `void main` in C++? [duplicate]

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

like image 763
kasperasky Avatar asked Jan 16 '09 08:01

kasperasky


People also ask

Why use int main instead of void Main?

The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().

Why don't we use void main in C?

In C++ the default return type of main is void, i.e. main() will not return anything. But, in C default return type of main is int, i.e. main() will return an integer value by default. In C, void main() has no defined(legit) usage, and it can sometimes throw garbage results or an error.

Why does C need int main?

int main represents that the function returns some integer even '0' at the end of the program execution. '0' represents the successful execution of a program. int main(void) represents that the function takes NO argument. Suppose, if we don't keep void in the bracket, the function will take any number of arguments.

Is it fine to write void main () or main () in C?

In that case, the value returned is 0, meaning successful execution. is an error because the return type of main() is missing. It is never a good idea to use “void main()” or just “main()” as it doesn't confirm standards.


2 Answers

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.

like image 107
Greg Hewgill Avatar answered Oct 17 '22 07:10

Greg Hewgill


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.
like image 45
abelenky Avatar answered Oct 17 '22 08:10

abelenky