ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a, const void *b) {
    return strcmp(*(const char *const *)a, *(const char *const *)b);
}
int main(void) {
    const char *w[] = {"pear", "apple", "fig", "apple2", "banana"};
    qsort(w, 5, sizeof w[0], cmp);
    printf("%s", w[1]);
}
  1. A. apple
  2. B. banana
  3. C. apple2
  4. D. fig

Correct answer: C. apple2

strcmp orders the strings as apple, apple2, banana, fig, pear, since a prefix sorts before its extensions. The element at index 1 is apple2.

More C Programming questions

Exams that ask this topic