What should the comparison function passed to qsort() return when its first argument should be placed before the second?
Correct answer D. A negative integer
Explanation
qsort calls the comparator with pointers to two elements and expects a negative value if the first sorts before the second, zero if they are equivalent and positive if it sorts after. Returning a positive value would reverse the order. Writing return a - b; for ints can overflow, so (a > b) - (a < b) is a safer way to produce the sign.
Answer checked How we write and check questions
Discussion