ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
long long x = *(const long long *)a, y = *(const long long *)b;
return (x > y) - (x < y);
}
int main(void) {
long long v[] = {9223372036854775807LL, -9223372036854775807LL, 0LL};
qsort(v, 3, sizeof v[0], cmp);
for (int i = 0; i < 3; i++) printf("%lld ", v[i]);
}- A. 0 -9223372036854775807 9223372036854775807
- B. -9223372036854775807 0 9223372036854775807
- C. 9223372036854775807 0 9223372036854775806
- D. -9223372036854775807 9223372036854775807 0
Correct answer: B. -9223372036854775807 0 9223372036854775807
The comparator returns only -1, 0 or 1, so it cannot overflow even for values near LLONG_MIN and LLONG_MAX. The sorted order is the mathematical order of the three values.
More C Programming questions
- What is printed? c #include <stdio.h> int calls = 0; int g(int a, int b) { calls++; return b ? g(b, a % b) : a; } int main(void) { int r =…
- How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion. c #include <stdio.h> long long…
- What is printed? c #include <stdio.h> int calls = 0; int bs(int a, int lo, int hi, int k) { calls++; if (lo > hi) return -1; int m = lo +…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0; int c = 0; for (unsigned i = 1; i <= 16; i++) { x = i; while (x) {…
- What is printed? c #include <stdio.h> unsigned alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xDEADu; printf("%u", (x >> 3) & 0xFu); }