A brief analysis of the difference between return and exit in c language

  • 2020-04-01 21:32:09
  • OfStack

1. Exit is used to terminate the program at any time during the program running. The parameters of exit are returned to the OS. The exit function is also implicitly called when the main function ends. The exit function runtime first executes the functions registered by the atexit() function, then does some cleanup of its own, flushing all output streams, closing all open streams, and closing temporary files created by the standard I/O function tmpfile(). Exit is the end of a process, which removes the memory space used by the process and returns the error message to the parent process, while return returns the value of the function and exits the function.

2. Return is language-level and represents the return of the call stack; Exit, on the other hand, is at the system call level, which represents the end of a process.

The exit function exits the application and returns a state of the application to the OS, which identifies some information about the application. It has to do with the machine and the operating system and it's usually 0 for normal exit and not 0 for abnormal exit

      Function prototype   Void exit (int status);   (header file stdlib.h)

4. Return is the return function call, if the return is the main function, then exit the program
Exit is to force the exit of a program at the point of call

Related articles: