一、头文件
C:#include<string.h>
C++:#include
二、基本类型
cout<<sizeof(char)<<endl; //1
cout<<sizeof(bool)<<endl; //1
cout<<sizeof(short)<<endl; //2
cout<<sizeof(int)<<endl; //4
cout<<sizeof(long)<<endl; //4
cout<<sizeof(float)<<endl; //4
cout<<sizeof(double)<<endl; //8
三、常量
cout<<sizeof(100)<<endl;
//4
cout<<sizeof(3.14)<<endl;
//8
const int b=0.618;
cout<<sizeof(b)<<endl;
//4
const double c=3.14;
cout<<sizeof(c)<<endl;
//8
define N 100
define p 3.14
cout<<sizeof(N)<<endl;
//4
cout<<sizeof(p)<<endl;
//8
cout<<sizeof("abc")<<endl;
//4,常量字符串后还有'\0'
三、指针
32位系统中指针都是4,64位系统中指针都是8
//64bits
cout<<sizeof(char*)<<endl; //8
cout<<sizeof(bool*)<<endl; //8
cout<<sizeof(short*)<<endl; //8
cout<<sizeof(int*)<<endl; //8
cout<<sizeof(long*)<<endl; //8
cout<<sizeof(float*)<<endl; //8
cout<<sizeof(double*)<<endl; //8
四、数组
sizeof(数组)=数组的元素个数*每个元素所占字节
int a[10];
cout<<sizeof(a)<<endl;
//40
//10*4
int a[10]={1,2,3};
cout<<sizeof(a)<<endl;
//40
//10*4
注意区分字符数组的strlen与sizeof
char str1[10]="sun";
str1[1]='\0';
cout<<sizeof(str1)<<endl;
//10
cout<<strlen(str1)<<endl;
//1,在第一个非'\0'前的非'\0'元素的个数
char *str2="sun";
cout<<sizeof(str2)<<endl;
//8,指针,win-64bits
cout<<strlen(str2)<<endl;
//3,在第一个非'\0'前的非'\0'元素的个数
char str3[]="sun";
cout<<sizeof(str3)<<endl; //4
//字符数组在内存分配充足时含有'\0'
char str4[3]={'s','u','n'};
cout<<sizeof(str4)<<endl; //3
//和规定的下标一样
参考:
https://www.cnblogs.com/lidabo/p/3618345.html
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询