In most cases int main()
does not return anything, it doesn't even have to since no return
would not give an error. So why does main have to return an int? Why is void main not possible?
EDIT: I meant, why is int main() the standard if there usually is no return
?
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.
In a main function, the return statement and expression are optional.
Yes, main() must return int . The return value is passed back to the operating system, to indicate whether the program ran successfully: zero means success. However, you can leave the return statement out of main (and only main ) if you like; in that case, it will return zero.
return 1: A return 1 means that there is some error while executing the program, and it is not performing what it was intended to do.
Other programs may use the return code to determine whether the application executed successfully. Zero typically means successful execution.
void
is possible but non-standard. The returned int
is meant to signify something for the caller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With