initial import
[staff/goc4/2018bti7061.git] / c-5-structuresFiles / examples / c-structures / unions.c
1 #include <stdio.h>
2 #include <string.h>
3
4 typedef struct
5 {int r; int g; int b;} color;
6
7 union val{
8 color col;
9 int size;
10 };
11
12
13 int main(){
14
15 union val array[10];
16 array[0].col=(color){255,0,0};
17 array[1].size=41;
18 array[2].col=(color){255,255,255};
19 array[3].size=39;
20 array[4].col=(color){0,255,55};
21 array[5].size=44;
22 array[6].col=(color){255,200,200};
23 array[7].size=42;
24 printf("Size of color :%lu\n",sizeof(color));
25 printf("Size of val :%lu\n",sizeof(union val));
26 printf("Size of int :%lu\n",sizeof(int));
27
28
29 int nb_items=4;
30 int i;
31 for(i=0;i<nb_items;i++){
32 printf("Color:(%d, %d, %d), size: %d \n",array[2*i].col.r,array[2*i].col.g,array[2*i].col.b,array[2*i+1].size);
33 }
34 puts("And if we mix inputs");
35 for(i=0;i<nb_items;i++){
36 printf(" Color:(%d, %d, %d), size: %d \n",array[2*i+1].col.r,array[2*i+1].col.g,array[2*i+1].col.b,array[2*i].size);
37 }
38
39 }
40 /* Output
41 Size of color :12
42 Size of val :12
43 Size of int :4
44
45 Color:(255, 0, 0), size: 41
46 Color:(255, 255, 255), size: 39
47 Color:(0, 255, 55), size: 44
48 Color:(255, 200, 200), size: 42
49
50 And if we mix inputs
51 Color:(41, 6, 0), size: 255
52 Color:(39, 4196224, 0), size: 255
53 Color:(44, 349646710, 32767), size: 0
54 Color:(42, 4196224, 0), size: 255
55 */