ProgrammingModerateIBM · Deloitte pattern
With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)?
With 4-byte int and 8-byte double aligned on 8 bytes, what is sizeof(union U)?
union U { int i; double d; char c[10]; };- A. 8
- B. 16
- C. 22
- D. 10
Correct answer: B. 16
A union is as large as its largest member (10 bytes for c), rounded up to a multiple of its strictest alignment (8, for double): 16. Adding the sizes (22) is what a struct would roughly need.
More C Programming questions
- What is printed? c int nextid(void) { static int id = 0; return ++id; } int main(void) { int a = nextid(), b = nextid(), c = nextid()…
- What is printed? c int x = 2; switch (x) { case 1: printf("1"); case 2: printf("2"); case 3: printf("3"); break; default: printf("D"); }
- What is printed? c if (-1 < 1u) printf("yes"); else printf("no");
- What is printed? c int a = 0, b = 0; int x = (++a > 5) && (++b > 0); int y = (++a > 0) || (++b > 0); printf("%d %d %d %d", a, b, x, y);
- What is printed? c printf("%c %d %d", 'A' + 2, 'A' + 2, 'z' - 'a');
- What is printed? c unsigned char a = 250, b = 10, z = 0; int s = a + b; unsigned char t = a + b; z--; printf("%d %d %d", t, z, s);