ProgrammingVery hard

A comparator for qsort is written as int cmp(const void *a, const void *b) { return ((const struct P *)a)->k > ((const struct P *)b)->k; }. Which statement is correct?

  1. A. It returns only 0 or 1 and never a negative value, so the sort cannot order elements that should go first
  2. B. It is correct, since greater-than gives the sign
  3. C. It is correct, but only when the array has two elements
  4. D. It is a compile error because > cannot compare ints

Correct answer: A. It returns only 0 or 1 and never a negative value, so the sort cannot order elements that should go first

qsort needs a negative, zero or positive result. A boolean returns 0 or 1, so the order it implies is incomplete and the result is wrong.

More C Programming questions

Exams that ask this topic