Question discussion

Assume a 4-byte int aligned on 4-byte boundaries. What is the output of the following C program?#include struct A { char a; int b; char c;…

1339Medium DSSSB TGT Computer Science 0 replies 1 views
0

Assume a 4-byte int aligned on 4-byte boundaries. What is the output of the following C program?

#include <stdio.h>
struct A { char a; int b; char c; };
union U { int i; char c[5]; };
int main() {
    printf("%zu %zu", sizeof(struct A), sizeof(union U));
    return 0;
}
  • A6 5
  • B12 8
  • C12 5
  • D8 8
Correct answer: B

Explanation

The struct is padded to 1 + 3 (pad) + 4 + 1 + 3 (pad) = 12 bytes; the union needs 5 bytes and is padded to a multiple of 4, i.e. 8.

Discussion 0

Nobody has replied yet. Start the discussion!