The format of the main function is correct.
The main function is called the main function, and a C program is always executed from the main () function. On the web pages and books of C language, you can see multiple formats of the main function. Some of these formats are correct and some are incorrect. To avoid errors, we will summarize them as follows.
I. main Function Header Format:
1. int main (int argc, char * argv [])
Recommendation index:★★★★★
There is a parameter format, which complies with the C89 and C99 standards. The first parameter, argc (argument count), is the number of strings in the command line. The second parameter argv (argument value) is a pointer array. Generally, the program name is assigned to argv [0], and each parameter is assigned values in turn starting from argv [1.
For example, run the command dir/a/p.
Then: argc = 3, argv [0] = dir, argv [1] =/a, argv [2] =/p
int main(int argc, char *argv[]){ return 0;}
2. int main (void)
Recommendation index:★★★★★
The format of the parameter is not supported. This is a method that complies with the C89 and C99 standards.
int main(void){ return 0;}
3. int main ()
Recommendation index:★★★☆☆
The parameter-free format is equivalent to 2nd writing methods. It is allowed in the C89 standard. Common compilers also support this method, but it does not comply with the latest C99 standard.
int main(){ return 0;}
4. main ()
Recommendation index:★☆☆☆☆
The format without parameters is equivalent to 3rd writing methods, which are allowed in the C89 standard, but does not comply with the latest C99 standard. The C language of the first version has only one int type, and there are no char, long, float, etc. Since there is only one type, you can leave it empty. Later versions of the C language are required to be compatible with the previous Code: if the return value is not explicitly indicated, the default return value is int, that is, main () is equivalent to int main (). In the C99 standard, the compiler must give at least a warning for the usage of main. Although this writing method can also be compiled, it is strongly recommended that you do not write it in a few letters.
main(){ return 0;}
5. void main ()
Recommendation index: ☆☆☆☆☆
This method is not recommended. In C and C ++, the function prototype that does not receive any parameters or return any information is "void foo (void );". This may be the reason why many people mistakenly believe that the main function can be defined as void main (void) If no program return value is required ). However, this is wrong! The Return Value of the main function should be defined as the int type, which is specified in the C and C ++ standards. Bjarne Stroustrup, the father of C ++, clearly stated in the FAQ on his homepage that the definition of void main () never exists in C ++ or C. In some compilers, void main can be compiled (such as vc6), but void main has never been defined in the standard. Therefore, if you want your program to be highly portable, use int main.
void main(){ return 0;}
Ii. main function return value:
The return Value Type of the main function is int type, and the return 0 at the end of the program is echo with it. 0 is the return value of the main function. So where will this 0 be returned? Return to the operating system, indicating that the program Exits normally.
If the return statement is not written at the end of the main function, C99 requires the compiler to automatically add return 0 to the generated target file, indicating that the program Exits normally. Note that the C-Free MinGW5 compiler automatically adds return 1 and VC6 does not add return 0 to the target file. Although some compilers, such as C-Free and VC6, can compile the return statement after the return statement is omitted, it is recommended that you add the return statement to develop a good habit of complying with the standard.