ProgrammingVery hard

A struct array holds records with an int key; the program calls qsort with the comparator (int)(a->key - b->key) for keys near INT_MAX and INT_MIN. What is the correct description of the behaviour?

  1. A. The result is always negative and the sort is correct
  2. B. Signed overflow is undefined, so the sign of the result cannot be relied on
  3. C. The result is always zero, so the sort does nothing
  4. D. The comparison is safe because the difference is cast to int

Correct answer: B. Signed overflow is undefined, so the sign of the result cannot be relied on

The subtraction itself overflows when the keys differ by more than INT_MAX, which is undefined behaviour. The cast does not fix it.

More C Programming questions

Exams that ask this topic