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);
}- A. 3 7
- B. 7 3
- C. 0 0
- 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
- What is printed? c int main(void) { int n = 6; int result = 1; for (int i = n; i > 1; i--) result = i; printf("%d", result); }
- What is printed? c int main(void) { int x = 5, y = 5; if (x = y) printf("true:%d", x); else printf("false:%d", x); }
- What is printed? c int main(void) { int a = 2, b = 3; int result = a++ b-- + --a + ++b; printf("%d %d %d", a, b, result); }
- What is printed? c int main(void) { int n = 4; int triangle = n (n + 1) / 2; printf("%d", triangle); }
- What is printed? c int main(void) { int x = 15; int y = 4; printf("%d %d %d", x / y, x % y, (x / y) y + (x % y)); }
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…