ProgrammingVery hardCompetitive coding (CP)

What is printed?

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

Correct answer: B. -9223372036854775807 0 9223372036854775807

The comparator returns only -1, 0 or 1, so it cannot overflow even for values near LLONG_MIN and LLONG_MAX. The sorted order is the mathematical order of the three values.

More C Programming questions

Exams that ask this topic