ProgrammingHard

int cmp(const void *a, const void *b) { return ((const struct Rec *)a)->k - ((const struct Rec *)b)->k; } Which statement is correct about this comparator with keys in [0, 100]?

  1. A. It is safe for these keys, since the subtraction cannot overflow
  2. B. It is undefined for every input because of the cast
  3. C. It returns the wrong sign because subtraction is reversed
  4. D. It is undefined for keys above 1000 only

Correct answer: A. It is safe for these keys, since the subtraction cannot overflow

With keys in [0, 100] the difference is small, so no overflow occurs. The subtraction gives a negative, zero or positive result as qsort expects.

More C Programming questions

Exams that ask this topic