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 first; int second; };
struct Pair makePair(int a, int b) {
    return (struct Pair){.first = a, .second = b};
}
int main(void) {
    struct Pair p = makePair(3, 7);
    printf("%d %d", p.first, p.second);
}
  1. A. 3 7
  2. B. 7 3
  3. C. 0 0
  4. D. Compilation error: compound literals with designated initializers are invalid

Correct answer: A. 3 7

`(struct Pair){.first = a, .second = b}` is a valid C99 compound literal with designated initializers, constructing an unnamed struct value on the spot; it's returned by value, so p.first=3, p.second=7.

More C Programming questions

Exams that ask this topic