Question discussion

What is the output of the following C program on a typical 64-bit system?#include struct S { char a; int b; char c; }; union U { int i; cha…

1190Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program on a typical 64-bit system?

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

Explanation

Padding makes struct S 12 bytes (1 + 3 pad + 4 + 1 + 3 pad); the union is 5 bytes rounded up to a multiple of its 4-byte alignment, i.e. 8.

Discussion 0

Nobody has replied yet. Start the discussion!