版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangjiaweiwei/article/details/85676248
int a,b,c,d,e,f,g,h,i,j,k,l,m;
typedef char int8;
int8 fourInt8[4] = {1,2,3,4};
int fourInt32[4] = {1,2,3,4};
const int x = 90;
int main(void)
{
a=sizeof(char);
b=sizeof(unsigned char);
c=sizeof(short int);
d=sizeof(unsigned short int);
e=sizeof(int);
f=sizeof(unsigned int);
g=sizeof(float);
h=sizeof(long);
i=sizeof(long long);
j=sizeof(double);
k=sizeof(long double);
l=sizeof(fourInt8);
m=sizeof(fourInt32);
return 0;
}
a、b、c、d、 e、 f、 g、 h、 i、 j 、k 分别用来获取char、 unsigned char、 short int、 unsigned short int、int、 unsigned int、float、long、long long、double、long double的长度。
l用来获取字符类型的数组fourInt8的长度,m用来获取整型类型的数组fourInt32的长度
编译完代码之后,将可执行文件下载到TLE9843_2QX芯片中,点击【D]进入debug模式,点击运行,调出Watch1窗口,查询
a、b、c、d、 e、 f、 g、 h、 i、 j 、k 、l、m的值
由运行结果可以推论,在TLE9843_2QX芯片平台上的基本数据类型的长度分别为:
基本数据类型 | 长度(bit) | 长度(byte) |
char | 8 | 1 |
unsigned char | 8 | 1 |
short int | 16 | 2 |
unsigned short int | 16 | 2 |
int | 32 | 4 |
unsigned int | 32 | 4 |
float | 32 | 4 |
long | 32 | 4 |
long long | 64 | 8 |
double | 64 | 8 |
long double | 64 | 8 |
而char类型的数组l的长度为4 bytes,int类型的数组长度为16 bytes。且两个数组的内容是一样的,所以如果想
节省空间,而且数组元素的值不超过一定范围(2^8),可以将数组的类型定义为char类型而不是int类型,这样
可以节省很大一部分Flash存储空间。