Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 2. Zadeklaruj w programie unię zawierającą pola różnych typów. Następnie napisz funkcję, która wy- pisze rozmiar tej unii na ekranie, oraz wypisze sumę rozmiaru wszystkich jej pól. Jak wytłumaczyć zjawisko, że nie zawsze wartości te są równe?
- */
- #include <stdio.h>
- union NewUnion{
- int var1;
- char var2;
- double var3;
- } example_union;
- void print_size(union NewUnion example_union)
- {
- unsigned long sum_of_all_vars = 0;
- sum_of_all_vars += sizeof(example_union.var1);
- sum_of_all_vars += sizeof(example_union.var2);
- sum_of_all_vars += sizeof(example_union.var3);
- printf("Size of all variables in union: %lu\n", sum_of_all_vars);
- printf("Size of union %lu\n", sizeof(example_union));
- }
- int main(void)
- {
- print_size(example_union);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement