ProgrammingModerateTCS NQT · Accenture

What is printed?

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

Correct answer: D. -3 -1 5

The comparator subtracts small values, which cannot overflow, so it sorts the numbers in ascending order: -3, -1, 5.

More C Programming questions

Exams that ask this topic