MyMSDN

MyMSDN记录开发新知道

C语言位域

这个概念显然是知道的,不过刚才忘了,然后写了个程序就明白了,记录一下,也许以后又健忘了:
 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

posted on 2009-12-30 01:02 volnet 阅读(770) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


特殊功能