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]);
}- A. 9 10 100
- B. 10 100 9
- C. 100 10 9
- 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
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = a, y = b; if (x[0] != y[0])…
- What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed? c #include <stdio.h> int…
- How many subsets of {1, 2, 3} (the empty set included) have a sum divisible by 3? The recursion below counts them. What is printed? c…
- How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program. c #include <stdint.h> #include…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 12; printf("%u", (x << 3) + (x << 1)); }
- How many adjacent pairs of 1 bits does 27 (binary 11011) have? The program counts the set bits of x & (x >> 1). c #include <stdio.h> int…