ProgrammingHardWipro · Cognizant pattern

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

Assume a 64-bit LP64 ABI. What is printed?
struct Pair { int first; int second; };
struct Pair makePair(int a, int b) {
    return (struct Pair){a, b};
}
int main(void) {
    struct Pair p = makePair(3, 4);
    printf("%d %d", p.first, p.second);
}
  1. A. 3 4
  2. B. 4 3
  3. C. Compile-time error: compound literals cannot be used in a return statement
  4. D. 0 0

Correct answer: A. 3 4

(struct Pair){a, b} is a compound literal (a C99 feature), creating an unnamed temporary struct value initialised with a and b, which is then returned by value, copying its contents to the caller. p.first=3, p.second=4.

More C Programming questions

Exams that ask this topic