ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(const int *)a - *(const int *)b;
}
int main(void) {
int v[] = {5, -3, -1};
qsort(v, 3, sizeof v[0], cmp);
for (int i = 0; i < 3; i++) printf("%d ", v[i]);
}- A. 5 -1 -3
- B. -1 -3 5
- C. -3 5 -1
- D. -3 -1 5
Correct answer: D. -3 -1 5
The comparator subtracts small values, which cannot overflow, so it sorts the numbers in ascending order: -3, -1, 5.
More C Programming questions
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { int a = -1; unsigned b = 2; printf("%u"…
- What is printed? c #include <stdio.h> int main(void) { unsigned k = 0; int len = -1; printf("%d", k > len); }
- How many integers in 0..15 are a power of two? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for (unsigned x…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {3, 1, 2}, swaps = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 2…
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint32t h = 5381; const char s =…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct R { int a, b; }; int cmp(const void p, const void q) { const struct R x =…