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]);
}
  1. A. 1 2 5 8 9
  2. B. 9 8 5 2 1
  3. C. 5 2 8 1 9
  4. 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

Exams that ask this topic