ProgrammingModerateProduct company OA
What is printed?
What is printed?
int cmp(const void *a, const void *b) {
return *(const int *)b - *(const int *)a;
}
int main(void) {
int v[] = {4, 9, 1, 7};
qsort(v, 4, sizeof v[0], cmp);
printf("%d %d", v[0], v[3]);
}- A. 1 9
- B. 9 1
- C. 4 7
- D. 9 4
Correct answer: B. 9 1
qsort puts a before b when the comparator returns a negative value. Computing b − a (rather than a − b) reverses the usual order, so the array is sorted descending: 9 7 4 1. (Subtraction is safe here only because the values are small.)
More C Programming questions
- What is printed? c printf("%d %d %d", atoi("42abc"), atoi(" -7x"), atoi("x9"));
- What is printed? c int a = 0, b = 0; int n = sscanf("12 abc 7", "%d %d", &a, &b); printf("%d %d %d", n, a, b);
- With 4-byte int, what is printed? c int p = (int[]){10, 20, 30}; p[1] += 5; printf("%d %zu", p[1] + p[2], sizeof((int[]){10, 20, 30}) /…
- What is printed? c #define COUNT(...) (sizeof((int[]){VAARGS}) / sizeof(int)) printf("%zu %zu", COUNT(1, 2, 3), COUNT(7));
- After the call below, what is c? c int c = 0; void perm(char s, int k, int n) { if (k == n) { c++; return; } for (int i = k; i < n; i++) {…
- What is printed? c int steps = 0; int gcd(int a, int b) { steps++; return b == 0 ? a : gcd(b, a % b); } int main(void) { int g = gcd(1071…