Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1427908
  • 博文数量: 842
  • 博客积分: 12411
  • 博客等级: 上将
  • 技术积分: 5772
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-14 14:43
文章分类

全部博文(842)

文章存档

2013年(157)

2012年(685)

分类: C/C++

2012-05-12 23:07:46




1.基本数据类型占用字节(32位的机器)

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. void BytePossess()
  4. {
  5.     printf(" char : %dByte\n",sizeof(char));
  6.     printf(" signed char : %dByte\n",sizeof(signed char));
  7.     printf(" unsigned char : %dByte\n",sizeof(unsigned char));
  8.     printf(" short int : %dByte\n",sizeof(short int));
  9.     printf(" unsigned short int : %dByte\n",sizeof(unsigned short int));
  10.     printf(" int : %dByte\n",sizeof(int));
  11.     printf(" signed int : %dByte\n",sizeof(signed int));
  12.     printf(" unsigned int : %dByte\n",sizeof(unsigned int));
  13.     printf(" long int : %dByte\n",sizeof(unsigned int));
  14.     printf(" unsigned long int : %dByte\n",sizeof(unsigned long int));
  15.     printf(" double : %dByte\n",sizeof(double));
  16.     printf("  float : %dByte\n",sizeof(float));
  17. }

点击(此处)折叠或打开

  1.                 char : 1Byte
  2.          signed char : 1Byte
  3.        unsigned char : 1Byte
  4.            short int : 2Byte
  5.   unsigned short int : 2Byte
  6.                  int : 4Byte
  7.           signed int : 4Byte
  8.         unsigned int : 4Byte
  9.             long int : 4Byte
  10.    unsigned long int : 4Byte
  11.               double : 8Byte
  12.                float : 4Byte
2:结构体:涉及内存对齐以提高内存的利用率,位段的使用

点击(此处)折叠或打开

  1. typedef struct
  2. {
  3.     char a;
  4.     int b;
  5.     char c;
  6. }Astruct;

  7. struct Bstruct
  8. {
  9.     char a;
  10.     char b;
  11.     int c;
  12. };

  13. struct Cstruct
  14. {
  15.     int x:1;
  16.     int y:14;
  17.     int Z:32;
  18.     int W:1;
  19. //    int z:33;//不可超过int 32的长度
  20. };

点击(此处)折叠或打开

  1. int main()
  2. {
  3.     BytePossess();
  4.     printf("----------分割线-------------\n");
  5.     char a=129;
  6.     unsigned char b=-1;
  7.     printf("%d\n",a);
  8.     printf("%d\n",b);
  9.     printf("----------分割线-------------\n");
  10.     Astruct A;//Astruct是typedef定义的新类型,用这新类型定义变量
  11.     struct Bstruct B;
  12.     struct Cstruct C;
  13.     printf("A:%dByte\n",sizeof(A));//32机器的内存是以4字节对齐的,char 4,int 4,char 4   总12
  14.     printf("B:%dByte\n",sizeof(B));//char char 两个占4,int 4  总8//提高了内存的利用率
  15.     printf("C:%dByte\n",sizeof(C));//位段:节省存储空间,还有好几个好处。自己百度,谷歌
  16.     return 0;
  17. }

点击(此处)折叠或打开

  1. ----------分割线-------------
  2. -127
  3. 255
  4. ----------分割线-------------
  5. A:12Byte
  6. B:8Byte
  7. C:12Byte
阅读(279) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~