Talking about the return value of C language return statement and main function

In the function, if the return statement is encountered, the program will return to execute the next statement that calls the function. That is to say, it will jump out of the execution of the function and return to the original place to continue execution. However, if the return statement is encountered in the main function, the entire program will stop and exit the execution of the program.

Return is a C++ predefined statement that provides an amplification of the crop function execution. When the return statement provides a value, this value becomes the function's return value.

Speaking of return, it is necessary to mention the definition of the main function. The following is the information found from the network. Take a good look at it, and it is very helpful for understanding the return value of the main function.

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++. The father of C++ Bjarne Stroustrup clearly wrote The definiTIon void main( ) { /* in the FAQ on his home page. . . */ } 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 the main function in the C and C++ standards.

Talking about the return value of C language return statement and main function

1. C

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 minor 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 be consistent with the variable naming rules.

If you do not need to get parameters from the command line, use int main(void); otherwise use int main( int argc, char *argv[] ).

The return type of the main function must be int so that the return value can be passed to the program's activation (eg, the operating system).

If the main function does not write a return statement at the end, C99 stipulates that the compiler should automatically add return 0; to the generated target file (exe file) to indicate that the program exits normally. However, I still recommend that you add a return statement to the end of the main function. Although this is not necessary, it is a good practice. Note that vc6 does not include return 0; in the target file, presumably because vc6 is a 98-year product, so this feature is not supported. Now understand why I suggest that you better add a return statement! However, gcc3.2 (C compiler under Linux) adds return 0; to the generated target file.

2. C++

C++98 defines the following two main function definitions:

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[] ) is also used as defined in C99. Similarly, the return type of the main function must also be an int. If no return statement is written at the end of the main function, C++98 specifies that the compiler should automatically add return 0; in the generated object file. Similarly, vc6 does not support this feature, but g++3.2 (C++ compiler under Linux) supports it.

3. About void main

In C and C++, the function prototype that does not receive any parameters nor return any information is "void foo(void);". It may be because of this, so many people mistakenly believe that if you do not need the program to return a value, you can define the main function as void main(void) . However this is wrong! The return value of the main function should be defined as an int, as specified in the C and C++ standards. Although in some compilers, void main can be compiled (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 compile at all. And gcc3.2 will issue a warning. So if you want your program to be very portable, be sure to use int main.

The role of the return value

The return value of the main function is used to indicate the exit status of the program. If it returns 0, it means the program exits normally, otherwise it means the program exits abnormally. Below we 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 type "echo %ERRORLEVEL%" and press Enter. You can see that the return value of the program is 0. Assume that the file you just compiled is a.exe. If you enter "a && dir", the folders and files in the current directory will be listed. However, if you change to "return -1" or something other than 0, enter "a && dir" after recompiling, then dir will not be executed. Because the meaning of && is: If the program preceding && exits normally, the program following && continues execution, otherwise it does not execute. That is, using the program's return value, we can control whether or not to execute the next program. This is the benefit of int main. If you are interested, you can change the return type of the main function to something other than an int (like float). After recompiling, execute "a && dir" and see what happens. Think about why this happens. By the way, if a || dir is entered, it means that if a exits abnormally, dir is executed.

5. What about int main(intargc,char*argv[],char*envp[])?

This is certainly not what is defined in standard C! Char*envp[] is an extension provided by some compilers to get system environment variables. Because it is not a standard, it is not supported by all compilers. Therefore, portability is poor and not recommended.

At this point, you should understand why the main function is defined as an int return type and there is a return 0 inside the function body; this statement is fine.

The following specifically talk about my understanding of the application of return.

As long as the return value of a function is numeric, then it can return 0 (that is, return 0), in fact, no matter how much you return. Under normal circumstances, the function that C++ makes requires to return a value. When the function is executed normally, and reaches the purpose of the general situation, then returning 0 means that the function is called correctly. This 0 is returned to the calling function. The notification was not wrong; if there was an error in the function call, or if it was not performed as usual, then 1 is returned to tell the calling function to take the response policy; if you define a header in the definition of the class in which a function is located Group state values ​​(usually negative integers), then the function can return different values ​​to inform the main call function of what abnormal or wrong, this situation is generally used in the function of poor function independence. Therefore, we generally do not encourage the return type of the function to be void, at least the return should be int, and add the return 0. statement at the end of the function:

Int func (parameter list)

{

......

......

......

Return 0;

}

In the function, if the return statement is encountered, the program will return to execute the next statement that calls the function. That is to say, it will jump out of the execution of the function and return to the original place to continue execution. However, if the return statement is encountered in the main function, the entire program will stop and exit the execution of the program.

If you define a function that has a return type, you can call it like this:

Int func()

{

Int value;

......

......

......

Return value;

}

Int main()

{

Int intvalue;

Intvalue=func();

......

......

Teturn 0;

}

What is the specific content behind the return statement, which requires a specific analysis of the specific circumstances:

(1) In a function whose return type is char, return should be of type char;

(2) In a function whose return type is int, if it is a call to stop the function, it is better to be 0; other according to your purpose, as long as it is an int type on the line

(3) In a function whose return type is a structure type, return should be an instance of the structure.

In short, the function defines what kind of return type the function should return after the corresponding type value.

11 Inch Laptop

Do you still operate 11 Inch Laptop Deals? If yes, here is the right place you should put more time and energy. You can see here 11 Inch Laptop in traditional standard or touch screen or 360 rotating. 11 Inch Windows Laptop in metal with 360 yoga, 11 Inch Touch Screen Laptop on 2 in 1 style, 11 Inch Laptop With 8gb Ram 128gb in plastic, etc. Believe you can find right one here for you. Of course, if have other special requirement prefer, just call us and share your demand details, thus we can send right and value information for you quickly. Sometimes, you may hesitate which storage is most suitable for your jobs? 256GB or 512GB SSD ROM provides huge storage space for big files, so that you can hold large documents and work your way through it freely. No worry lack of storage any more. N5100 CPU can enhance the overall performance for office, children students, daily entertainment, etc.

Any other style prefer, just contact us and share your demand, then we can know how to do more for you.

11 Inch Laptop,11 Inch Windows Laptop,11 Inch Touch Screen Laptop,11 Inch Laptop Deals,11 Inch Laptop With 8gb Ram

Henan Shuyi Electronics Co., Ltd. , https://www.sycustomelectronics.com