A brief analysis of C + + exit and return

Source: Internet
Author: User
Tags exit in

"Summary"
This article is from the code form. Frequently used methods, related concepts, call relationships and comparative analysis, these 5 dimensions analyze the same points and differences between exit and return in C + +.


"Common form"

Exit (0): Execute the program normally and exit the program.
Exit (1): Non-normal execution results in exiting the program;
Return (): Returns a function that, if in the main function, exits the function and returns a value.

"Related Concepts"
    • The return statement can be used to return the executing branch program back to the calling method. Represents aborting the execution of the current function. and return the operation right to the caller, assuming it is in the main function. Represents the right to return the operation to the operating system.
Assume that you want to exit if a certain condition is met. Can return with return, otherwise can not write this code, when the program runs to "}" will be self-return, the problem in fact, you have to try on the machine to know.

Just as a good programming habit, each function has a return statement is right, so more readable, and conducive to the maintenance of the program!


The return instruction is one that returns a specified data to the key function, and the other is to end the run of the function,returning no need to return a value.

    • The exit () function is used to end the program at any time during program execution, and the exit parameter state is returned to the operating system. Returning 0 indicates that the program ends normally, and not 0 indicates that the program is not properly terminated. The Exit function is also implicitly called at the end of the main function. When the Exit function executes, the function that is enlisted by the atexit () function is executed first. It then does some cleanup work of its own, flushing all output streams at the same time, closing all open streams, and closing temporary files created by standard I/O functions tmpfile ().

Another

Each process will have a return value. The process starts with the main function that is dropped by a startup function of the system:
int nmainretval = Main ();
When you exit from the main function. The start function calls the Exit function and passes the NMAINRETVA to it. So. Whenever the Exit function is called, normally. The main function does not call the Exit function, but is returned by return 0; return value to Nmainretval, exit then receive this value as the parameter. So, normally exit with exit (0).


Assume. Your program has an exception, you can call exit (1) in the main function, force quit the program, and force the process to terminate. Non 0 indicates an abnormal exit.


"Comparative analysis"
1, return returns the function value, is Keyword;exit is a function.


2, return is the language level. It represents the return of the call stack, and exit is the system call level. It represents the end of a process.


3. Return is the exit of the function (return); Exit is the exit of the process.


4. Return is provided by the C language, and exit is provided by the operating system (or given in the function library).
5, return is used to end a function of the run, the function of the running information out of the other calling function use; Exit function exits the application. Deletes the memory space used by the process and returns a state of the application to the OS, which identifies some of the application's operational information, which is related to the machine and the operating system, usually 0 for normal exit. Non-0 is a non-normal exit.
6. Calling return and exit in a non-main function is a very obvious effect. However, the phenomenon of calling return and exit in the main function is very vague. In most cases, the phenomenon is consistent.

A brief analysis of C + + exit and return

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.