ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
    unsigned x = *(const unsigned *)a, y = *(const unsigned *)b;
    return (y > x) - (y < x);
}
int main(void) {
    unsigned v[] = {4000000000u, 5u, 3000000000u};
    qsort(v, 3, sizeof v[0], cmp);
    for (int i = 0; i < 3; i++) printf("%u ", v[i]);
}
  1. A. 5 3000000000 4000000000
  2. B. 4000000000 3000000000 5
  3. C. 3000000000 4000000000 5
  4. D. 4000000000 5 3000000000

Correct answer: B. 4000000000 3000000000 5

The comparator returns a positive value when the second argument is larger, which sorts the unsigned values in descending order. Using a signed subtraction here would wrap around for these values.

More C Programming questions

Exams that ask this topic