liveBook · Manning

Exit

As explained in Modern C

exit is the C standard library function that terminates normal program execution after performing the standard termination processing.

A helper can terminate normal execution by calling exit with the status that main would return: _Noreturn void fail(void) { exit(EXIT_FAILURE); } Calling fail does not return to its caller. It terminates the program as though main had evaluated return EXIT_FAILURE. section 7.2

Use exit deliberately when termination must occur outside main:

  • Prefer a regular return from main for ordinary program termination. section 8.7
  • Call exit from a function that must terminate regular control flow; exit never returns and is treated as equivalent to returning from main. section 7.2 section 8.7
  • Pass EXIT_SUCCESS or EXIT_FAILURE when expressing success or failure portably. section 7.2
  • Allow normal termination processing to perform runtime cleanup, including flushing or closing files and releasing resources. section 8.7
  • Reserve other terminating functions for cases that specifically require suppressing library cleanup. section 8.7
_Noreturn void exit(int status);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":14}],[{\"line\":0,\"ch\":15},{\"line\":0,\"ch\":19}],[{\"line\":0,\"ch\":20},{\"line\":0,\"ch\":23}]]"}
!@%STYLE%@!

Inspect the declaration showing that exit accepts an int status, returns void, and is marked _Noreturn. section 7.2

How Modern C covers it

How deep it goes
Defines the prototype, status equivalence, nonreturning behavior, and cleanup implications, but defers implementation details.
Examples
Shows exit declarations, main status handling, and an atexit cleanup handler that reports termination.
What it assumes
Assumes familiarity with main and return, while deferring detailed treatment of function parameters and function pointers.
What you can do after
Apply it to terminate execution from a nested function while communicating success or failure and preserving normal cleanup.
How it connects
Connects exit with main, return statements, termination status, atexit, quick_exit, _Exit, abort, and resource cleanup.

Also explained in

Compare how 2 books explain exit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.