1.基本数据类型占用字节(32位的机器)
- #include<stdio.h>
- #include<stdlib.h>
- void BytePossess()
- {
- printf(" char : %dByte\n",sizeof(char));
- printf(" signed char : %dByte\n",sizeof(signed char));
- printf(" unsigned char : %dByte\n",sizeof(unsigned char));
- printf(" short int : %dByte\n",sizeof(short int));
- printf(" unsigned short int : %dByte\n",sizeof(unsigned short int));
- printf(" int : %dByte\n",sizeof(int));
- printf(" signed int : %dByte\n",sizeof(signed int));
- printf(" unsigned int : %dByte\n",sizeof(unsigned int));
- printf(" long int : %dByte\n",sizeof(unsigned int));
- printf(" unsigned long int : %dByte\n",sizeof(unsigned long int));
- printf(" double : %dByte\n",sizeof(double));
- printf(" float : %dByte\n",sizeof(float));
- }
- char : 1Byte
- signed char : 1Byte
- unsigned char : 1Byte
- short int : 2Byte
- unsigned short int : 2Byte
- int : 4Byte
- signed int : 4Byte
- unsigned int : 4Byte
- long int : 4Byte
- unsigned long int : 4Byte
- double : 8Byte
- float : 4Byte
2:结构体:涉及内存对齐以提高内存的利用率,位段的使用
- typedef struct
- {
- char a;
- int b;
- char c;
- }Astruct;
- struct Bstruct
- {
- char a;
- char b;
- int c;
- };
- struct Cstruct
- {
- int x:1;
- int y:14;
- int Z:32;
- int W:1;
- // int z:33;//不可超过int 32的长度
- };
- int main()
- {
- BytePossess();
- printf("----------分割线-------------\n");
- char a=129;
- unsigned char b=-1;
- printf("%d\n",a);
- printf("%d\n",b);
- printf("----------分割线-------------\n");
- Astruct A;//Astruct是typedef定义的新类型,用这新类型定义变量
- struct Bstruct B;
- struct Cstruct C;
- printf("A:%dByte\n",sizeof(A));//32机器的内存是以4字节对齐的,char 4,int 4,char 4 总12
- printf("B:%dByte\n",sizeof(B));//char char 两个占4,int 4 总8//提高了内存的利用率
- printf("C:%dByte\n",sizeof(C));//位段:节省存储空间,还有好几个好处。自己百度,谷歌
- return 0;
- }
- ----------分割线-------------
- -127
- 255
- ----------分割线-------------
- A:12Byte
- B:8Byte
- C:12Byte
阅读(3882) | 评论(0) | 转发(0) |