Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.