ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
union U { char c; int i; double d; };
printf("%zu", sizeof(union U));- A. 13
- B. 8
- C. 4
- D. 16
Correct answer: B. 8
A union's size is the size of its largest member (double, 8 bytes) rounded up to the strictest alignment among members (also 8 for double), so sizeof(union U) is 8.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Inner { char c; int i; }; struct Outer { int x; struct Inner in; double d; }…
- On a typical x86-64 GCC/Clang target, what is printed? c struct F { unsigned a : 20; unsigned b : 20; }; printf("%zu", sizeof(struct F));
- Assume a 64-bit LP64 ABI. What is printed? c struct S { char c; int arr[2]; }; printf("%zu %zu", sizeof(struct S), offsetof(struct S, arr));
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));
- What is printed? c #pragma pack(push, 1) struct Packed { char c; int i; }; #pragma pack(pop) printf("%zu", sizeof(struct Packed));
- Assume a 64-bit LP64 ABI and 1-byte Bool. What is printed? c struct S { Bool b; double d; }; printf("%zu", sizeof(struct S));