A qsort comparator for int returns (a - b) and is used on values close to INT_MIN and INT_MAX. Why is this a defect?
A
The comparator must return only 0 or 1, and any other return value is ignored by the qsort implementation
B
Signed overflow in the subtraction is undefined behaviour, so the sign of the result can be wrong
Correct answer
C
qsort requires the comparator to return a long rather than an int, so the subtraction must be widened
D
qsort calls the comparator once per element, so ties between equal keys are never detected at all
Correct answer
B. Signed overflow in the subtraction is undefined behaviour, so the sign of the result can be wrong
14 views
⚔️
Challenge a friend on C Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
Overflow-safe comparison is required because INT_MAX minus INT_MIN cannot be represented in int, and signed overflow is undefined behaviour that can invert the ordering. The portable form is (a > b) - (a
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussionNot sure why the answer is B? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ A buffer is grown with p = realloc(p, 2 * n); and the call fails. What is the correct statement? Next MCQ › What does printf return when it succeeds?