retagged by
636 views
1 votes
1 votes

Consider the following C declaration 

struct 
{ 
    short s[5]; 
    union 
    { 
        float y; 
        long z; 
    }u; 
}t;

Assume that objects of the type short, float and long occupy $2$ bytes, $4$ bytes and $8$ bytes respectively. The memory requirement for variable $t$ ignoring alignment considerations, is 

  1. $22$ bytes
  2. $14$ bytes
  3. $18$ bytes
  4. $10$ bytes
retagged by

1 Answer

3 votes
3 votes

 In union, all members share the same memory location.

How is the size of union decided by compiler?

Size of a union is taken according the size of largest member in union.

Size $= 8 + 2*5 = 18$ Bytes

C is correct. 

Answer could have been different if "memory requirement for variable t ignoring alignment consideration" wasn't mentioned.

Answer:

Related questions

2 votes
2 votes
2 answers
2
admin asked Mar 30, 2020
1,088 views
Output of following program#include<stdio.h int main() { int i=5; printf("%d %d %d", i++,i++,i++); return 0; }$7\:6\:5$$5\:6\:7$$7\:7\:7$Compiler Dependent
3 votes
3 votes
1 answer
3
admin asked Mar 30, 2020
1,764 views
Output of following program? #include<stdio.h void dynamic(int s,...) { printf("%d",s); } int main() { dynamic(2,4,6,8); dynamic(3,6,9); return 0; }$2\:3$Compiler Error$4...