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