ProgrammingHardWipro · Cognizant pattern
What happens?
What happens?
int compare(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int main(void) {
int arr[] = {5, 2, 8, 1, 9};
qsort(arr, 5, sizeof(int), compare);
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
}- A. 1 2 5 8 9
- B. 9 8 5 2 1
- C. 5 2 8 1 9
- D. 1 9 2 8 5
Correct answer: A. 1 2 5 8 9
qsort sorts in place using the comparator; compare(a,b) returns *a - *b, which is positive when a>b, negative when a<b — this sorts ascending: 1 2 5 8 9.
More C Programming questions
- Assume 32-bit int. What is printed? c unsigned int a = 0; a--; printf("%u", a);
- Assume 32-bit int. What is printed? c int flags = 0b1010; int mask = 0b0110; printf("%d %d", flags & mask, flags | mask);
- Assume 32-bit int. What is printed? c int x = 100; int y = x << 1; int z = x >> 1; printf("%d %d", y, z);
- Assume 32-bit int. What is printed? c int a = 12, b = 10; int x = a ^ b; int y = x ^ b; int z = x ^ a; printf("%d %d", y, z);
- Assume 32-bit int. What is printed? c int x = 0xFF; int y = x & 0x0F; int z = (x >> 4) & 0x0F; printf("%d %d", y, z);
- Assume 32-bit int. What is printed? c unsigned int a = 5; int b = -3; printf("%d", a + b > 0);