In this program, we are going to share a C program to find the size of a union. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to find the size of a union with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> void main() { union sample { int m; float n; char ch; }; union sample u; printf("The size of union = %d\n", sizeof(u)); /* initialization */ u.m = 25; printf("%d %f %c\n", u.m, u.n, u.ch); u.n = 0.2; printf("%d %f %c\n", u.m, u.n, u.ch); u.ch = 'p'; printf("%d %f %c\n", u.m, u.n, u.ch); } |
The size of union = 4
25 0.000000
1045220557 0.200000
1045220464 0.199999
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.