iT邦幫忙

0

【C++】Data Type Size Of

c++
  • 分享至 

  • xImage
  •  

這次我們要來學習資料型態在程式中的大小,亦即調查其所占的空間

我列出一些常用的data type~ unsigned就先不放了lol~ 因為太複雜


學習目標: 資料型態大小的概念

學習難度: ☆☆☆


https://ithelp.ithome.com.tw/upload/images/20220417/20147886MP4snT847V.png


#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/


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言