liveBook · Manning

Exit

As explained in Modern C, Third Edition

exit is a C standard-library function that terminates normal program execution and performs implementation-defined cleanup for the process.

exit terminates normal program execution and returns a status to the runtime system. Calling exit(s) has the same termination effect as returning s from main; exit never returns to its caller. section 7.2

Terminate after an unrecoverable output failure

if (puts("hello world") == EOF) { perror("can't output to terminal:"); exit(EXIT_FAILURE); } Check the library function's return value, report the failure, and terminate with EXIT_FAILURE. section 8.1.3

Use exit according to the control-flow situation:

  • Prefer return from main for ordinary program termination. section 8.8
  • Use exit inside a function that must terminate normal control flow rather than return through its callers. section 8.8
  • Pass EXIT_SUCCESS or EXIT_FAILURE when portable success and failure status values are sufficient. section 7.2
  • Allow normal cleanup to run; avoid more abrupt termination functions unless inhibiting library cleanup is necessary. section 8.8
[[noreturn]] void exit(int status);

Displays the nonreturning function prototype for exit. section 7.2

How Modern C, Third Edition covers it

How deep it goes
Defines termination equivalence, nonreturning behavior, status values, cleanup, and error-handling use, but not detailed handler mechanics.
Examples
Uses failed puts output, perror diagnostics, EXIT_FAILURE, and the exit prototype as concrete examples.
What it assumes
Assumes familiarity with functions, main, return values, library error checks, and standard status macros.
What you can do after
Apply it to terminate from nested control flow after diagnosing an unrecoverable library failure.
How it connects
Connects exit with main, return statements, program termination, library cleanup, error handling, perror, and atexit.

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, Third Edition. The section links point to the parts of the book it draws on.