liveBook · Manning

Atexit

As explained in Modern C, Third Edition

atexit is a C standard-library function that registers a function to be called during normal program termination.

atexit registers a function for execution during normal program termination. The registered function is not called when passed: atexit(sayGoodBye) passes a reference to sayGoodBye. Such handlers can free memory, write a termination timestamp, or perform other cleanup. section 8.8

Registering a termination handler

void sayGoodBye(void) { if (errno) perror("terminating with error condition"); fputs("Good Bye\n", stderr); } int main(int argc, char* argv[argc+1]) { atexit(sayGoodBye); ... } After normal termination, sayGoodBye executes and reports the execution status. section 8.8

int atexit(void func(void));
int at_quick_exit(void func(void));

Use the declaration to see that atexit receives a function parameter returning void and returns an int. section 8.8

How Modern C, Third Edition covers it

How deep it goes
Defines registration and function-reference syntax, but deliberately omits detailed handler mechanics.
Examples
Demonstrates a sayGoodBye handler that reports errors and writes a farewell message.
What you can do after
Apply atexit to register memory cleanup or termination logging before normal program completion.
How it connects
Contrasts normal termination with exit, quick_exit, at_quick_exit, _Exit, and abort.

Also explained in

Compare how 2 books explain atexit →

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.