Machine-translated by Google. Questions are authored in English.
Top

Discussion

Predict the output of the program below. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.

union test
{
    int x;
    char arr[8];
    int y;
};
 
int main()
{
    printf("%d", sizeof(union test));
    return 0;
}

 

  • A.12
  • B.16
  • C.8
  • D.Compiler Error

Answer: C

Correct answer: 8

A union is as large as its largest member. Here int x and int y are 4 bytes each, and char arr[8] is 8 - so the union is 8 bytes. That the two ints are separate members makes no difference: all three overlay the same storage.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.