forked from ksw2000/Data-Structure-in-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.c
43 lines (32 loc) · 1 KB
/
union.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
/*
union 跟 struct 很像
主要的區別在 union 內的成員只能選一個
另外 union 的大小由裡面暫最大空間的那個決定
詳情可參考
http://gundambox.github.io/2015/10/30/C%E8%AA%9E%E8%A8%80-struct%E3%80%81union%E3%80%81enum/
*/
union u{
char c;
int n;
float f;
};
int main(){
union u foo;
foo.c='c';
printf("foo.c: %c\n", foo.c);
printf("foo.n: %d\n", foo.n); //無效(殘值)
printf("foo.f: %e\n", foo.f); //無效(殘值)
printf("---------\n", foo.f);
foo.n=87;
printf("foo.c: %c\n", foo.c); //無效(殘值)
printf("foo.n: %d\n", foo.n);
printf("foo.f: %e\n", foo.f); //無效(殘值)
printf("---------\n", foo.f);
foo.f=6.626e-34;
printf("foo.c: %c\n", foo.c); //無效(殘值)
printf("foo.n: %d\n", foo.n); //無效(殘值)
printf("foo.f: %e\n", foo.f);
//%e 表示以科學記號方式印出 (因為 普朗克常數有點太小)
return 0;
}