Exit () and return

Source: Internet
Author: User
Tags exit in

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.

Detailed said:  

1. Return returns the function value, which is the keyword; exit is a function.

2. Return is a language level that 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, using the function's execution information out of other calling functions;exitfunction exits the application, deletes the memory space used by the process, and returns a state of the application to the OS. This status identifies some of the application's operational information, which is related to the machine and the operating system, typically 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 blurred, and in most cases the phenomenon is consistent.

The difference between exit () and return in C + +

Exit ():

Code # include <iostream>using namespace Std;class temp{public:    Temp () {cout<< "constructor!" <<endl;}    ~temp () {cout<< "destructor!" <<endl;}}; int main () {       Temp t;    Exit (0);}

constructor!
Press any key to continue

Return

Code # include <iostream>using namespace Std;class temp{public:    Temp () {cout<< "constructor!" <<endl;}    ~temp () {cout<< "destructor!" <<endl;}}; int main () {       Temp t;    return 0;}

constructor!
destructor!
Press any key to continue

Comprehensive:

Exit () quit the program and give control to the OS
Return ends the current function, returns the value of the function, and gives control to the calling function
Return is similar to exit in the main function, but return automatically calls the object's destructor, and exit does not

What is the meaning of the parameters of the exit () function in C/

Ask:

1) I know 0 means normal exit, not 0 means false exit, but what does this parameter mean?

2) It is said that the exit () function is not recommended in object-oriented, and if so, what is the method for replacing it?

For:

The design of C language is designed for Unix systems, and this system is "a lot of programs with each other" paired into a system.

Each running program is a process, and the process will have a parent process, the parent process is usually the direct start of your process, the parent process of death will be adopted by the INIT, the parent process becomes init, and the parent process of Init is process 0, and process 0 is the first process started at system startup.

The argument inside exit () is passed to its parent process. For the parent process, your process is like a function, and the function can have a return value.

1) What is the meaning of this parameter:

The argument for exit () is used by the parent process itself. Usually a program's parent process may be any process, so we cannot expect our parent process to stipulate that the return value must be required, so we should provide this return value to ensure that the requirements of the different parent processes are met.

A typical example is that Make,makefile has multiple sequential statements for a target, and make as the parent process checks whether the return value of each statement is 0, and any non-0 value will stop the execution of the current rule. And we know that make can actually execute any command any program, so any program that is called by make must have the correct return value.

2) Why to use the exit () function:

In fact, in most systems, the main function is not the real entry point for the application.

The real entry point is a function in the C Runtime Library, which is responsible for doing some initialization before calling main. When main returns to the entry function in the runtime, it also does some cleanup work.

If any of the platforms must call exit to exit gracefully, then the platform will have to enclose exit in the C run-time library and let main return with exit to "normal exit".

The value of the Exit function is to terminate the program's operation directly in a nested function when you do not want to or are unwilling to return to main. Whether this is good or not, that's another question. Anyway I'm personally disgusted with "end in call stack" like this exit mode.

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.