ProgrammingVery hardGATE CS · TCS NQT
What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in place?
What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in place?
#include <stdio.h>
struct In { int i; char d; };
struct Out { char c; struct In in; };
int main(void) {
printf("%zu", sizeof(struct Out));
}- A. 12
- B. 10
- C. 9
- D. 16
Correct answer: A. 12
struct In is 8 bytes after padding, and it must start at offset 4 to be int-aligned after the char c. The outer struct is 4 + 8 = 12 bytes.
More C Programming questions
- What is printed? c #include <stdio.h> long long sum(long long n, long long acc) { return n == 0 ? acc : sum(n - 1, acc + n); } int…
- How many odd multiples of 3 are there in 1 to 30? Count them with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { int m[2][2] = {{3, 1}, {4, 2}}; int s = 0; for (int i = 0; i < 2; i++) { int lo =…
- What is printed? c #include <stdio.h> int main(void) { int r0[2] = {1, 2}, r1[2] = {3, 4}; int rows[2] = {r0, r1}; int t = rows[0]…
- What is printed? The reverse of the binary digits of 13 (1101) is taken as an integer. c #include <stdio.h> int main(void) { unsigned x =…
- How many occurrences of the two-letter string "ab" are in "abcabab"? Count them with this program. c #include <stdio.h> int main(void) {…