Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is int main(void) valid in C++?

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?

like image 540
geza Avatar asked Jul 01 '17 09:07

geza


People also ask

Is int main valid in C?

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.

Can I use void main in C?

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 is main int not void?

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.

Why do we write int main void in C?

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.


1 Answers

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.

like image 138
Edgar Rokjān Avatar answered Oct 11 '22 02:10

Edgar Rokjān