ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int compareInts(const void *a, const void *b) {
    int ia = *(const int *)a;
    int ib = *(const int *)b;
    return (ia > ib) - (ia < ib);
}
int main(void) {
    int arr[] = {4, 1, 3, 2};
    qsort(arr, 4, sizeof(int), compareInts);
    printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
}
  1. A. 1 2 3 4
  2. B. 4 3 2 1
  3. C. 4 1 3 2
  4. D. 2 1 3 4

Correct answer: A. 1 2 3 4

compareInts implements the standard branchless three-way comparison: positive when ia>ib, negative when ia<ib, zero when equal — qsort uses this to sort ascending: 1 2 3 4.

More C Programming questions

Exams that ask this topic