Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
struct x
{
int i;
char c;
}

union y
{
struct x a;
double d;
};
printf("%d",sizeof(union y));

Options :-
a)8
b)5
c)4
d)1
Posted
Comments
phil.o 30-Sep-15 7:47am    
You really should do your homework by yourself.
Jochen Arndt 30-Sep-15 7:48am    
Write a small program, compile it, and run it.
Krunal Rohit 30-Sep-15 7:48am    
8.
-KR

8.
Because, double is size of 8, so the union size will be 8 bytes (largest one).
int will occupy 2 or 4 bytes(compiler dependent) and char will 1 bytes, which makes the total of 3 or 5.
The memory occupied by a union will be large enough to hold the largest member of the union.

-KR
 
Share this answer
 
v2
Comments
nv3 30-Sep-15 17:49pm    
Who says int is always 2 or 4 bytes? On 64-bit machines it is often enough 8 bytes long. And makes struct x longer than 8 bytes.
Krunal Rohit 8-Oct-15 0:30am    
I didn't say always. :D
I mentioned it's compiler dependent & forgot to write machine dependent as well. ( Solution #2)
No less than 8, with very high probably 8, however machine (and compiler) dependent.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900