1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A call to strtol succeeds and returns a valid long. What…

Added 30 Nov -0001

A call to strtol succeeds and returns a valid long. What must a careful program do before checking errno to detect overflow?

  1. Nothing; a successful call always leaves errno set back to zero
  2. Call clearerr first, since errno is only defined for stream functions
  3. Nothing; errno is only meaningful when the function returns -1 or NULL
  4. Set errno to 0 beforehand, because success never clears it by itself Correct answer

Correct answer

D. Set errno to 0 beforehand, because success never clears it by itself

17 views

MCQSEXAM

Explanation

errno is never cleared by a successful library call: it is set to a nonzero code only on failure, so a stale value from an earlier failure can masquerade as a new error. The correct idiom is errno = 0 before the call, then to test the return value and errno together. That is why strtol's end pointer and ERANGE must be examined as a pair rather than trusting errno alone.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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