ProgrammingVery hardTCS NQT · Infosys
What is printed? Assume signed char is 8 bits, two's complement.
What is printed? Assume signed char is 8 bits, two's complement.
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(const signed char *)a - *(const signed char *)b;
}
int main(void) {
signed char v[] = {127, -56, 5};
qsort(v, 3, sizeof v[0], cmp);
for (int i = 0; i < 3; i++) printf("%d ", v[i]);
}- A. -56 5 127
- B. 5 127 -56
- C. 127 5 -56
- D. -56 127 5
Correct answer: A. -56 5 127
The comparator subtracts two signed char values, and both promote to int before the subtraction, so the order is the signed order -56, 5, 127. Treating the bytes as unsigned would put -56 (the byte 200) last.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { long long x = (const long long )a, y =…
- 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) {…