#include stdiohunion myun struct int x y z; u; int k; a;int main aux = 4; auy = 5; auz = 6; ak = 7; printfdn aux; printfdn sizeofa; return 0;
The output of this program will be:
6 12
Explanation:
- We declare a union called
myunwhich contains a struct with three integersx,y, andz, as well as a single integerk. - In the
mainfunction, we assign values toa.u.x,a.u.y, anda.u.z, as well asa.k. - When we print
a.u.x, we get the value of6instead of4because the value ofa.koverwrote the values ofa.u.x,a.u.y, anda.u.z. - The
sizeof(a)statement prints the size of the uniona, which is12bytes since thestructinside the union occupies 3 integers, each taking up 4 bytes
原文地址: https://www.cveoy.top/t/topic/fFFm 著作权归作者所有。请勿转载和采集!