Many people and even some books on the market use void main(), which is actually wrong. void main( ) has never been defined in C/C++. Bjarne Stroustrup, the father of C++, clearly stated in the FAQ on his homepage that The definition void main( ) { /* ... */ } is not and never has been C++, nor has it even been C.( void main ( ) never existed in C++ or C). Let me talk about the definition of main function in the C and C++ standards respectively.
1. Main() in C language
In C89, main() is acceptable. Brian W. Kernighan and Dennis M. Ritchie's classic masterpiece The C programming Language 2e ("C Programming Language Second Edition") uses main(). However, in the latest C99 standard, only the following two definitions are correct:
int main(void)
int main(int argc, char *argv[])
(Reference: ISO/IEC 9899: 1999 (E) Programming languages ? C 5.1.2.2.1 Program startup)
Of course, we can also make some small changes. For example: char *argv[] can be written as char **argv; argv and argc can be changed to other variable names (such as intval and charval), but they must comply with the naming rules of variables.
If you do not need to obtain parameters from the command line, please use int main(void); otherwise, please use int main(int argc, char *argv[]).
The return value type of the main function must be int, so that the return value can be passed to the caller of the program (such as the operating system).
If there is no return statement at the end of the main function, C99 stipulates that the compiler should automatically add return 0; to the generated target file (such as the exe file), indicating that the program exits normally. However, I still recommend that you add a return statement at the end of the main function. Although it is not necessary, it is a good habit. Note that vc6 will not add return 0; to the target file. This is probably because vc6 was a product in 1998, so it does not support this feature. Now you understand why I suggest you add a return statement! However, gcc3.2 (C compiler under Linux) will add return 0; to the generated object file.
2. Main() in C++
C++98 defines the following two main function definition methods:
int main( )
int main(int argc, char *argv[])
Reference: ISO/IEC 14882 (1998-9-01) Programming languages ? C++ 3.6 Start and termination
int main() is equivalent to int main(void) in C99; int main(int argc, char *argv[ The usage of ]) is also the same as defined in C99. Similarly, the return value type of the main function must also be int. If there is no return statement at the end of the main function, C++98 stipulates that the compiler should automatically add return 0; to the generated object file. Similarly, vc6 does not support this feature, but g++3.2 (the C++ compiler under Linux) does.
3. About void main()
In C and C++, the prototype of a function that does not receive any parameters or returns any information is "void foo(void);". Perhaps because of this, many people mistakenly believe that if the program does not need to return a value, the main function can be defined as void main(void). But this is wrong! The return value of the main function should be defined as int type, which is stipulated in the C and C++ standards. Although void main can be compiled in some compilers (such as vc6), not all compilers support void main because void main has never been defined in the standard. In g++3.2, if the return value of the main function is not of type int, it will not pass compilation at all. gcc3.2 will issue a warning. Therefore, if you want your program to be highly portable, be sure to use int main.
Don’t excuse yourself with words like “My teacher told me it was the right thing to do”; teachers have a bad habit of being wrong. By writing safe, standards-compliant code, everyone can focus on other issues in your program instead of wasting time on this standard stuff.
It should be pointed out that in some systems, if the program is defined using void main or has no return value, it may cause a stack exception and cause a system failure. (See the English part below for details)
4. The function of the return value
The return value of the main function is used to indicate the exit status of the program. If 0 is returned, it means that the program exits normally; the meaning of other numbers returned is determined by the system. Normally, a non-zero return indicates that the program exited abnormally. Let's do a small experiment in the winxp environment. First compile the following program:
int main(void)
Then enter "echo %ERRORLEVEL%" and press Enter, you can see that the return value of the program is 0. Assume that the file just compiled is a.exe. If you enter "a && dir", the folders and files in the current directory will be listed. But if you change it to "return -1", or another non-zero value, and then recompile and enter "a && dir", dir will not be executed. Because the meaning of && is: if the program before && exits normally, the program after && will continue to be executed, otherwise it will not be executed. In other words, using the return value of the program, we can control whether to execute the next program. This is the benefit of int main. If you are interested, you can also change the return value type of the main function to a non-int type (such as float), recompile and execute "a && dir" to see what happens and think about why that happens. By the way, if you enter a || dir, it means that if a exits abnormally, dir will be executed.
5. What about int main(int argc, char *argv[], char *envp[])?
Of course this is not something defined in standard C/C++! char *envp[] is an extended function provided by some compilers for obtaining system environment variables. Because it is not a standard, not all compilers support it, so it has poor portability and is not recommended - unless your program is specifically designed to work in a specific environment and needs to obtain the system's environment variables.