1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Three handlers are registered with atexit in the order h1…

Added 30 Nov -0001

Three handlers are registered with atexit in the order h1, h2, h3, and then main returns normally. In what order do the handlers run?

  1. Only the last registered handler h3 runs when main returns normally
  2. h3, then h2, then h1, since handlers run in reverse registration order Correct answer
  3. h1, then h2, then h3, since handlers run in registration order exactly
  4. The order is unspecified, so any permutation of the three is conforming

Correct answer

B. h3, then h2, then h1, since handlers run in reverse registration order

14 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs