这次我们要来学习资料型态在程式中的大小,亦即调查其所占的空间。
我列出一些常用的data type~ unsigned就先不放了lol~ 因为太複杂
学习目标: 资料型态大小的概念
学习难度: ☆☆☆
#include <iostream>using namespace std;void DataTypeSize();int main() {DataTypeSize();}void DataTypeSize(){ char cha[] = "I am wei tsung"; char *p1=cha; char **p2=&p1; string name="wei tsung"; cout<<"cha[] size is "<<sizeof(cha)<<" bytes"<<"\n"<<endl; //15 bytes (null、阵列最后会有一个空个都算1byte) cout<<"char *p1 size is "<<sizeof(*p1)<<" bytes"<<"\n"<<endl; //1 byte cout<<"char **p2 size is "<<sizeof(**p2)<<" bytes"<<"\n"<<endl; //1 byte cout<<"string name size is "<<sizeof(name)<<" bytes"<<"\n"<<endl; //8 bytes cout<<"short int size is "<<sizeof(short int)<<" bytes"<<"\n"<<endl; //2 bytes cout<<"int size is "<<sizeof(int)<<" bytes"<<"\n"<<endl; //4 bytes cout<<"long long int size is "<<sizeof(long long int)<<" bytes"<<"\n"<<endl; //8 bytes cout<<"float size is "<<sizeof(float)<<" bytes"<<"\n"<<endl; //4 bytes cout<<"double size is "<<sizeof(double)<<" bytes"<<"\n"<<endl; //8 bytes cout<<"double size is "<<sizeof(longdouble)<<" bytes"<<"\n"<<endl; //12 bytes cout<<"wchar_t size is "<<sizeof(wchar_t)<<" bytes"<<"\n"<<endl; //2 bytes}
参考资料:
https://www.geeksforgeeks.org/c-data-types/