main () following two definitions:
int main ()
{
/ * * /
return 0;
}
and
int main (void)
{
/ * * /
return 0;
}
any different ?
is the same in C++. Both definitions of
can be used in C, but technically, the second definition using void is considered technically better because it clearly states that main can only be called without any parameters.
In C language, if the function signature does not specify any parameters, it means that the function can be called with any number of parameters or no parameters. For example, try to compile and run the following two C programs (remember to save the file as .c). Note the difference between the two signatures of fun(). The program above
compiles and runs normally, but the program below fails to compile
, which is different from C. In C++, neither of the above two programs can be compiled. In C++, fun() and fun(void) are the same.
So the difference is that in C language, you can use any number of parameters to call int main(), but you can only call int main(void) without using any parameters. Although there is no difference in most cases, it is recommended to use "int main(void)" in the C language.
exercise: (predict the output of the following C program)
question
question
friends understand? Hope it helps you!
-
3:26
-
17:45
-
1:11
-
2:18
-
2:10:48
-
4:36