ProgrammingHardProduct company OA · GATE CS
Assume 4-byte int with 4-byte alignment. What is printed?
Assume 4-byte int with 4-byte alignment. What is printed?
union U { char c[5]; int i; };
printf("%zu", sizeof(union U));- A. 5
- B. 4
- C. 9
- D. 8
Correct answer: D. 8
A union is as large as its largest member (5 bytes for c) but its size must also be a multiple of its strictest alignment (4, from int), so it is padded to 8. '5' forgets the alignment; '9' adds the members as a struct would.
More C Programming questions
- Compiled as C99 or later, what is printed? c int i = 1; for (int i = 5; i < 7; i++) { int i = 10; printf("%d ", i); } printf("%d", i);
- What is printed? c printf("%zu", sizeof("1234"));
- What is printed? c int a[8] = {0}; for (int i = 1; i < 8; i++) a[i] = a[i / 2] + (i % 2); printf("%d %d %d", a[5], a[6], a[7]);
- What is printed? c int a[] = {2, 1, 3, 2, 4, 1}, best = 0, s = 0, lo = 0; for (int hi = 0; hi < 6; hi++) { s += a[hi]; while (s > 6) s -=…
- What is wrong with this code? c char p = malloc(10); p++; free(p);
- What is printed? c int x = 5; #define x (x + 1) printf("%d", x);