Follow these steps to create a recipe that registers a function that executes automatically when a program terminates normally:
- Register a function using the atexit function.
- Allocate some memory dynamically and allow that memory to be pointed to by a pointer.
- Ask the user to enter a string and assign the string to the dynamically allocated memory block.
- Display the entered string on the screen.
- When the program terminates, the function registered via the atexit function is invoked automatically.
- The registered function simply frees up the dynamically allocated memory so that it can be used by other applications.
The program for registering a function that automatically executes when a program terminates is as follows ( ...