Difference between exit () and return

Source: Internet
Author: User
The difference between exit () and return: According to ansi c, return and exit () are used in the originally called Main () with the same effect. However, note that "initial call" is used here ". If main () is in a recursive program, exit () will still terminate the program; but return will hand over control to the first level of recursion until the first level, at this time, return will terminate the program. Another difference between return and exit () is that it terminates the program even if exit () is called in a function other than main. _ Exit () differs from Exit: header file: Exit: # include <stdlib. h> _ Exit: # include <unistd. h> _ exit () function: directly stop the process, clear its memory space, and destroy various data structures in the kernel. Exit () the functions are encapsulated based on these and several processes are added before execution and exit. The biggest difference between the exit () function and the _ exit () function is that the exit () function checks the file opening before calling the exit system, and writes the content in the File Buffer back to the file.

1. exit is used to end a running program. It returns the parameter to the OS and gives control to the operating system. Return is to exit the current function, return the function value, and give control to the calling function.
2. Exit indicates the system call level. It indicates the end of a process, while return indicates the language level. It indicates the return of the call stack.
3. At the end of the main function, the exit function is called implicitly. Therefore, when the program runs at the end of main (), the main process is terminated. Exit deletes the memory space used by the process and returns the error message to the parent process.
4. Void exit (INT status); Generally, status is 0, indicating normal exit, and non-0 indicates abnormal exit.

 

1. The main differences between the exit and return functions are:

1) exit is used to end the program at any time while the program is running. Its parameters are returned to the OS. You can also say that the exit function exits the application and returns a state of the application to the OS, which identifies some running information of the application.

The exit function is also called implicitly at the end of the main function. When the exit function is run, it first executes the function registered by the atexit () function and then cleans itself, refresh all output streams, close all open streams, and close temporary files created using the standard I/O function tmpfile.

Exit indicates the completion of a process at the system call level. It deletes the memory space used by the process and returns the error message to the parent process. Generally, exit (0) indicates that the program is normal, exit (1) and exit (-1) indicate that the program exits abnormally, and exit (2) indicates that the system cannot find the specified file. In the entire program, you only need to call exit to end.

 

2) return indicates the return of the Call Stack. Return indicates the return of the function value and exit the function. Generally, 0 indicates normal exit, and non-0 indicates abnormal exit. Please note that, if it is in the main function, it will naturally end the current process (that is, in Main (), you can use return N or exit (n) directly ), if it is not in the main function, it is returned to the previous call. In multiple processes, if you want to check whether the previous process Exits normally, you need to use the return value of the previous process.

Difference between 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.