ProgrammingHardTCS NQT · Wipro

What is printed?

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

Correct answer: A. 9 10 100

The comparator converts each string with atoi, so the order is numeric: 9, 10, 100. Plain strcmp would instead put 10 and 100 before 9.

More C Programming questions

Exams that ask this topic