1 #include <stdio.h> 2 3 struct struct_a 4 { 5 int a; 6 int b; 7 }; 8 9 enum Weekday 10 { 11 Mon, 12 Tue, 13 sat, 14 sun 15 }; 16 17 union bigend 18 { 19 int a; 20 char c; 21 }; 22 23 int main() 24 { 25 struct_a sa; 26 sa.b = 2; 27 sa.a = 1; 28 29 printf("struct %d\n",sa.b); 30 31 Weekday wd; 32 wd = sun; 33 34 printf("enum %d\n", wd); 35 36 bigend be; 37 be.a = 97; 38 39 printf("union %c\n",be.c); 40 return 0; 41 }
三者用法相似,enum限定了取值范围,union更多作用应该是在以void*类型拷贝数据后,按内部类型做具体解析。
时间: 2024-10-06 06:22:37