这个概念显然是知道的,不过刚才忘了,然后写了个程序就明白了,记录一下,也许以后又健忘了:
1 #include <stdio.h>
2 typedef struct _SimpleType1 {
3 int Variable1; //4bytes(32bits)
4 int Variable2; //4bytes(32bits)
5 int Variable3; //4bytes(32bits)
6 int Variable4; //4bytes(32bits)
7 } SimpleType1;
8
9 typedef struct _ComplexType1 {
10 int Variable1 : 8; //1bytes(8bits)
11 int Variable2 : 8; //1bytes(8bits)
12 int Variable3 : 8; //1bytes(8bits)
13 int Variable4 : 8; //1bytes(8bits)
14 } ComplexType1;
15
16 typedef struct _ComplexType2 {
17 int Variable1 : 8; //1bytes(8bits)
18 int Variable2 : 8; //1bytes(8bits)
19 int Variable3 : 8; //1bytes(8bits)
20 int Variable4 : 8; //1bytes(8bits)
21 int Variable5 : 1; //0.125bytes(1bits) but the it also hold 32bits
22 } ComplexType2;
23
24 int main(void){
25 printf("sizeof SimpleType1 = %d\n", sizeof(SimpleType1));
26 printf("sizeof ComplexType1 = %d\n", sizeof(ComplexType1));
27 printf("sizeof ComplexType2 = %d\n", sizeof(ComplexType2));
28 }
结果:
sizeof SimpleType1 = 16
sizeof ComplexType1 = 4
sizeof ComplexType2 = 8