博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sizeof运算符来获取各种数据类型在内存中所占字节数--gyy整理
阅读量:4647 次
发布时间:2019-06-09

本文共 881 字,大约阅读时间需要 2 分钟。

C++并没有规定各种数据类型在内存中的存储大小,依赖于不同的编译器的不同而不同,要想获知当前编译器对各种数据类型分配的大小,可以通过sizeof运算符来获取。

使用方法1:

sizeof(数据类型)  

使用方法2:

sizeof(变量名   或 常量名 或 表达式  )

sizeof(int)     

int  a;

sizeof(a)

 

[cpp] 
 
 
  1. //数据类型空间分配情况  
  2. #include <iostream>  
  3. using namespace std;  
  4. int main()  
  5. {    
  6.    cout<<"vc++6.0 编译环境下,各种数据类型变量所占的内存空间大小(字节为单位)"<<endl;  
  7.    cout<<"sizeof(int)  "<<sizeof(int)<<endl;  
  8.    cout<<"sizeof(short int)  "<<sizeof(short)<<endl;  
  9.    cout<<"sizeof(long int)  "<<sizeof(long)<<endl;  
  10.    cout<<"sizeof(unsigned int)  "<<sizeof(unsigned)<<endl;  
  11.    cout<<"sizeof(unsigned short int)  "<<sizeof(unsigned short)<<endl;  
  12.    cout<<"sizeof(unsigned long int)  "<<sizeof(unsigned long)<<endl;  
  13.    cout<<"sizeof(char )  "<<sizeof(char)<<endl;  
  14.    cout<<"sizeof(float)  "<<sizeof(float)<<endl;  
  15.    cout<<"sizeof(double)  "<<sizeof(double)<<endl;  
  16.    cout<<"sizeof(long double)  "<<sizeof(long double)<<endl;  
  17.    return 0;  
  18. }  

运行结果

 

 

转载于:https://www.cnblogs.com/beautiful-code/p/5239538.html

你可能感兴趣的文章
Python爬虫实战四之抓取淘宝MM照片
查看>>
2015 Multi-University Training Contest 1
查看>>
C#判断一个字符串是否是数字或者含有某个数字
查看>>
SVN使用指南
查看>>
【转载】掌 握 3 C ‧ 迎 接 亮 丽 职 涯
查看>>
爬取网站附件
查看>>
java基础图形界面和IO系统
查看>>
javascript学习笔记
查看>>
hdu 3996
查看>>
python第三十九课——面向对象(二)之初始化属性
查看>>
python学习笔记之函数装饰器
查看>>
FEM计算2D瞬态热传导方程
查看>>
四年时光,匆匆而过
查看>>
【php】【psr】psr1 基础编码规范
查看>>
WAF SSI
查看>>
LDAP & it's implementation
查看>>
Apache HttpComponents中的cookie匹配策略
查看>>
冰封的海盗攻略
查看>>
Netty4.x中文教程系列(四) 对象传输
查看>>
linux下find命令使用举例、
查看>>