Return is a pre-defined C ++ statement that provides an amplification function for execution. When the return statement provides a value, this value becomes the return value of the function. when it comes to return, it is necessary to mention the definition of the main function. many even some books on the market use void main (), which is actually incorrect. Void main () has never been defined in C/C ++ (). The father of C ++ Bjarne stroustrup clearly states the definition void main (){
/*... */} Is not and never has been c ++, nor has it even been c. (void main () never exists in C ++ or C ). The following describes the definition of the main function in the C and C ++ standards respectively.
1. c In c89, main () is acceptable. Brian W. kernighan and Dennis M. Ritchie use main (), the classic masterpiece of the C programming language 2e (second edition of C programming language (). 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 versions-C 5.1.2.2.1 program startup) Of course, we can also make a small change. 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 variable naming rules. If you do not need to obtain parameters from the command line, use int main (void); otherwise, 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 activator (such as the operating system) of the program ). If the return statement is not written at the end of the main function, c99 requires that the compiler automatically add return 0 to the generated target file (such as the EXE file), indicating that the program Exits normally. However, I suggest you add the return statement at the end of the main function. Although this is not necessary, it is a good habit. Note that vc6 does not include return 0; in the target file. This feature is probably not supported because vc6 is a 98-year product. Now I understand why I suggest you add
Return Statement! However, gcc3.2 (C compiler in Linux) will add return 0 to the generated target file ;.
2. the following two main functions are defined in C ++ 98: int main () int main (INT argc, char * argv []) (reference: ISO/IEC 14882 () programming ages-C ++ 3.6 start and termination) int main () is equivalent to int main (void) in c99; int main (INT argc,
Char * argv []) is also used as defined in c99. Similarly, the return value type of the main function must be Int. If the return statement is not written at the end of the main function, C ++ 98 requires the compiler to automatically add return 0 to the generated target file ;. Similarly, vc6 does not support this feature, but G ++ 3.2 (c ++ compiler in Linux) does.
3. In C and C ++, void main does not receive any parameters or return any information. The function prototype 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. In some compilers, void main can be compiled (such as vc6), but not all compilers support void
Main, because the standard has never defined void main. In G ++ 3.2, if the return value of the main function is not of the int type, it cannot be compiled. Gcc3.2 issues a warning. Therefore, if you want your program to be highly portable, use int main.
4. Function of return value the return value of main function is used to indicate the exit status of the program. If 0 is returned, the program Exits normally. Otherwise, the program exits abnormally. Next we will do a small experiment in the WINXP environment. First, compile the following program: int main (void) {return 0 ;}then open the "command prompt" in the attachment and run the compiled executable file in the command line, then input "Echo % errorlevel %" and press enter to see that the return value of the program is
0. Assume that the compiled file is a.exe. If "A & dir" is entered, the folders and files in the current directory are listed. However, if it is changed to "Return-1", or other non-0 values, enter "A & dir" after re-compilation, the Dir will not be executed. The meaning of & is: if the program before & Exits normally, continue to execute & subsequent programs; otherwise, do not execute. That is to say, 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 main function's return value type to a non-int type (such as float), re-compile and execute"
& Dir ", to see what will happen, think about why it will happen. By the way, if a | DIR is input, it indicates that if a exits abnormally, DIR is executed.
5. What about int main (intargc, char * argv [], char * envp? This is certainly not defined in standard C! Char * envp [] is an extension provided by some compilers to obtain system environment variables. Because it is not a standard, not all compilers support it. Therefore, it has poor portability and is not recommended. At this point, you should understand why the main function is defined as the int return type, and there is return 0 in the function body.
The following describes my understanding of the return application. As long as the return value of a function is numeric, 0 (return 0) can be returned. In fact, there is no problem with how much you return. Generally, all functions made by C ++ require that a value be returned. When the function is executed normally, if 0 is returned, it indicates that the function is called correctly, and 0 is returned to the main function to notify the function that no error has occurred. If an error occurs in the function call or the function is not executed normally, 1 is returned to inform the main function to adopt a response policy. If you define a group of Status values (generally negative integers) in the header file where the class definition of a function is located ), then the function can return different values to indicate the specific exceptions or errors of the function. This situation is generally used when the function independence is poor. Therefore, we generally do not encourage you to define the function return type as void. At least the return type should be int, and add return at the end of the function.
0. Statement: int func (parameter list ){...... ...... ...... Return 0;} in a function, if a return statement is encountered, the program will return the next statement that calls the function for execution, that is, the execution of the function will jump out and go back to the original place to continue the execution. However, if you encounter a return statement in the main function, the entire program will stop and exit the execution of the program. If you define a function with a return type, you can call it as follows: int func () {int value ;...... ............ Return Value;} int
Main () {int intvalue; intvalue = func ();...... ...... Teturn 0;} What is next to the Return Statement? This requires a detailed analysis: (1) In a function whose return type is Char, return should be a char value; (2) In a function whose return type is int, if you want to stop the function call, it is best to set it to 0; otherwise, it depends on your purpose, as long as it is int type (3) in a function whose return type is structure type, return should be an Instance Object of the structure.
In short, the return type defined by the function should be the value of the corresponding type after return in the function.