ProgrammingVery hardProduct company OA

Assume ASCII. What is printed?

Assume ASCII. What is printed?
int cmp(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}
int main(void) {
    const char *w[] = {"pear", "Apple", "fig", "apple"};
    qsort(w, 4, sizeof w[0], cmp);
    printf("%s %s", w[0], w[3]);
}
  1. A. fig pear
  2. B. Apple fig
  3. C. Apple pear
  4. D. pear Apple

Correct answer: C. Apple pear

qsort hands the comparator pointers to the array elements, which are themselves char pointers, hence the cast to const char ** and one dereference. strcmp compares character codes, and in ASCII every capital letter sorts before every small one: Apple, apple, fig, pear.

More C Programming questions

Exams that ask this topic