Three handlers are registered with atexit in the order h1, h2, h3, and then main returns normally. In what order do the handlers run?
Correct answer
B. h3, then h2, then h1, since handlers run in reverse registration order
14 views
B. h3, then h2, then h1, since handlers run in reverse registration order
14 views
Handlers registered with atexit are called in reverse order of registration, so the last one registered runs first: h3, h2, h1. All registered handlers run when main returns or when exit is called, but not when abort or _Exit terminates the process. Calling exit from inside a handler, or registering more than the implementation's documented limit, is undefined behaviour.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.