C++ standard lists allowed forms of main
. It doesn't list int main(void)
as an allowed form. But, it generally states that
The parameter list (void) is equivalent to the empty parameter list
Is int main(void)
an allowed form?
Is int main; a valid C program? Yes. A freestanding implementation is allowed to accept such program. main doesn't have to have any special meaning in a freestanding environment.
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().
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.
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. Actually, both seem to be the same but, int main(void) is technically better as it clearly mentions that main can only be called without any parameter.
From N3936 standard draft:
3.6 Start and termination
3.6.1 Main function
2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a declared return type of type int, but otherwise its type is implementation-defined. An implementation shall allow both
— a function of () returning int and
— a function of (int, pointer to pointer to char) returning int
as the type of main (8.3.5).
Then:
8.3.5 Functions
4 ... A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list. ...
Consequently,
int main(void)
is an allowed form of main
function.
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