ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
struct Pair { int a, b; };
struct Pair swapPair(struct Pair p) {
    int tmp = p.a;
    p.a = p.b;
    p.b = tmp;
    return p;
}
int main(void) {
    struct Pair p1 = {1, 2};
    struct Pair p2 = swapPair(p1);
    printf("%d %d %d %d", p1.a, p1.b, p2.a, p2.b);
}
  1. A. 2 1 1 2
  2. B. 1 2 2 1
  3. C. 2 1 2 1
  4. D. 1 2 1 2

Correct answer: B. 1 2 2 1

swapPair receives p BY VALUE (a copy of p1), swaps its own local copy's fields, and returns that modified copy; p1 itself is completely untouched (1,2), while the returned p2 holds the swapped values (2,1).

More C Programming questions

Exams that ask this topic