A brief analysis of C + + exit and return

Source: Internet
Author: User
Tags exit in

"Summary"
This
article from the Code form, common way, related concepts, call relations and comparative analysis, the 5 dimensions of the exit and return in C + + the same point and difference.


"Common form"

Exit (0): Run the program normally and exit the program;
Exit (1): Abnormal operation 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. Indicates that the current function is aborted and the operation is returned to the caller, and if it is in the main function, the operation is returned to the operating system.
If a certain condition to exit, you can return with return, or you can not write this code, when the program executes to "}" will be automatically returned, the problem is actually you on the machine to try to know. However, 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 the specified data to the key function, and the other function is to end the execution of the function, andreturndoes not have to return a value.

    • The exit () function is used to end the program at any time while the program is running, the exit parameter state is returned to the operating system, return 0 indicates that the program ends normally, and non 0 indicates that the program is not properly terminated. The Exit function is also implicitly called at the end of the main function. The Exit function runs with a function that is enlisted by the atexit () function, and then does some of its own cleanup work, flushing all output streams, closing all open streams, and closing temporary files created by standard I/O functions tmpfile ().

Another

Each process will have a return value, and the process starts off with the main function by a startup function of the system:
int nmainretval = Main ();
When exiting from the main function, the start function calls the Exit function and passes the NMAINRETVA to it. So, at any time, the Exit function is called, and normally, the main function does not call the Exit function, but is returned by return 0; Returns the value to Nmainretval, exit receives the value as a parameter. So, normally exit with exit (0).
If your program has an exception, you can call exit (1) in the main function, force the program to quit, and force the process to terminate. Non 0 indicates an abnormal exit.

"Comparative analysis"
1, return returns the function value, is the keyword; exit is a function.
2. Return is the language level, which represents the return of the call stack, and exit is the system call level, which 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 the execution of a function, the execution information of the function out of the other calling function use; the 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. This information is related to the machine and the operating system, usually 0 for normal exit, not 0 for abnormal exit.
6. Calling return and exit in a non-main function is obvious, but the phenomenon of calling return and exit in the main function is very vague, and in most cases the phenomenon is consistent.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A brief analysis of C + + exit and return

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.