0
How its union size 12 ?
#include <stdio.h> #include <string.h> int main() { union check { int num; char name[10]; }test; printf ("%ld",sizeof (test)); return 0; }
3 Antworten
+ 3
Size of a union is the size of its biggest member, a member that requires bytes most. Plus some padding bytes to round the allocated memory size up to the nearest multiple of 4 bytes. In this case the biggest member is <name> which requires 10 bytes, rounded up to 12, the next multiple of 4.
Hth, cmiiw
+ 1
I believe it has something to do with an integer filling up the union's size to the nearest multiple of 4.
Every character takes up a single byte and the integer fills up to the nearest multiple.
In your case, the int fills up to 12.
Try changing the amount of characters in the array.
+ 1
Thanks brothers i understood..