Exit in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The exit function in c is included in the stdlib.h header file. The exit function terminates the currently running program explicitly which called it. The exit() function flushes all the buffers used by the program, closes all the programs associated with the program calling it, and deletes all the temporary files associated.

Syntax of exit() Function in C

The syntax for the exit function in c is as below,

Parameters of exit() Function in C

The exit function in c takes the status as a parameter. Status indicates how the program terminated. The status can be of three types viz,

Value of parameterDescription
EXIT_FAILUREProgram terminated unsuccessfully
1Program terminated unsuccessfully
EXIT_SUCCESSProgram terminated successfully
0Program terminated successfully
  • 0 or EXIT_SUCCESS: The statements exit(EXIT_SUCCESS) and exit(0) mean that the program has terminated successfully without any errors or interruptions.
  • 1 or EXIT_FAILURE: The statements exit(1) and exit(EXIT_FAILURE) mean that the program terminated abruptly with an error. For example, Suppose we create a function to write a few lines in a word file. But the word file is not present, the program will encounter an error and the program stops abruptly. If we make use of the exit function in c, the function terminates without disturbing the whole program.

Note

In the syntax of the exit function in c, we saw that the exit function in c takes an integer as a parameter. But we are passing EXIT_SUCCESS and EXIT_FAILURE. This is because they are int valued macro in layman's terms, they are treated as an integer by the compiler.

Return Value of exit() Function in C

The exit function in c returns void, i.e, the exit function does not return anything.

Example

Code

Output

Explanation In the above code,

  • A for loop runs from i=0 to 7.
  • In the output, we expect the numbers from 0 to 7. But as we can see in the result, we get numbers from 0 to 4.
  • This is because in for loop, we have used the exit function.
  • When the value of i in the loop is 5, the if condition is executed i.e, exit(0).
  • Once the program encounters exit(0) the program terminates immediately.

What is exit() Function in C?

The exit function in c is defined under the stdlib.h header file is used to terminate the function currently running. The fuction also terminates all the programs which are running, flushes all the file buffers, closes all the stream and deletes all the temporary files.

The syntax of the exit function in c is simple. The function returns nothing and hence it is of void type. The exit function in c takes an integer or an int valued macro as a parameter.

More Examples

Exit Failure

Exit Failure is indicated by passing 1 or EXIT_FAILURE as an argument. To understand exit failure, let us look at an example.

code

output

Explanation

The above code opens the file File.txt in r (read-only) mode. If the file is not found, then the file is opened and then the statement Job completed is printed by the program.

In the above case, the file File.txt does not exist therefore the program enters the if part of the program and prints File not found, and exits the program because exit(EXIT_FAILURE) is called by the program.

Conclusion

  • The exit function in c is included under the stdlib.h header file in c.
  • The exit function returns void.
  • The exit function takes an integer or an int-valued macro as the parameter.
  • The exit function terminates the running program.