ProgrammingModerateIBM · Deloitte pattern
On a typical compiler where int is 4 bytes and 4-byte aligned, what is printed?
On a typical compiler where int is 4 bytes and 4-byte aligned, what is printed?
struct S { char c; int i; char d; };
struct T { int i; char c; char d; };
printf("%zu %zu", sizeof(struct S), sizeof(struct T));- A. 6 6
- B. 12 8
- C. 12 12
- D. 8 8
Correct answer: B. 12 8
In S, c takes byte 0, three padding bytes put i at 4–7, d sits at 8, and trailing padding rounds the size up to a multiple of 4: 12. In T, i is at 0–3, c and d at 4 and 5, then padding to 8. Ordering members from largest to smallest saves space.
More C Programming questions
- With 4-byte int, consider int (p)[5]; Which is correct?
- int a[3][4]; is stored in row-major order starting at address 1000, with 4-byte int. What is the address of a[2][1]?
- What is printed? c unsigned char c = 250; c += 10; printf("%d", c);
- What does f(3) print? c void f(int n) { if (n > 0) { f(n - 1); printf("%d ", n); f(n - 1); } }
- What is printed? c char a[] = "hi", b[] = "hi"; if (a == b) printf("same"); else printf("different");
- What is printed? c #define STR(x) #x #define CAT(a, b) a##b int xy = 7; printf("%s %d", STR(x+y), CAT(x, y));