A call to strtol succeeds and returns a valid long. What must a careful program do before checking errno to detect overflow?
Correct answer
D. Set errno to 0 beforehand, because success never clears it by itself
17 views
D. Set errno to 0 beforehand, because success never clears it by itself
17 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.