ProgrammingHardProduct company OA

What is printed?

What is printed?
struct E { char n; int age; } e[] = {{'A', 30}, {'B', 25}, {'C', 35}};
int cmp(const void *x, const void *y) {
    return ((const struct E *)x)->age - ((const struct E *)y)->age;
}
int main(void) {
    qsort(e, 3, sizeof e[0], cmp);
    printf("%c%c%c", e[0].n, e[1].n, e[2].n);
}
  1. A. ABC
  2. B. CAB
  3. C. CBA
  4. D. BAC

Correct answer: D. BAC

qsort moves whole structs (sizeof e[0] each) and orders them by the comparator, here ascending age: 25 (B), 30 (A), 35 (C). 'CAB' would need the comparison reversed.

More C Programming questions

Exams that ask this topic