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]);
}- A. fig pear
- B. Apple fig
- C. Apple pear
- 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
- What is printed? c int g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}; int mp(int r, int c) { if (r == 0 && c == 0) return g[0][0]; if (r < 0…
- What is printed? c unsigned next(unsigned x) { unsigned c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } int main(void) {…
- What is printed? c const char s = "abcabcab"; const char p = strstr(s, "cab"); printf("%td %s %td", p - s, p + 3, strstr(p + 1, "cab") - s);
- What is printed? c struct A { int v[3]; }; struct A make(void) { struct A a = {{1, 2, 3}}; return a; } int main(void) { struct A x =…
- What is printed? c const char s = "a1b2c3d4"; int sum = 0; for (; s; s++) { if (s >= 'a') continue; if (s == '3') break; sum += s - '0'; }…
- What is printed? c int s = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; case 3: break; default: s += 10; } s += i; }…