C++各变量类型所占字节数

本文通过C++代码展示了各种基本数据类型(如bool、char、整数、浮点数等)在计算机中的存储大小、位数以及它们的取值范围,帮助读者理解不同类型在内存占用上的差异。
摘要由CSDN通过智能技术生成

听说有人想知道各变量类型所占字节数,所以去我就特意写了这些代码。

#include<iostream>  
#include<string>  
#include <limits> 
#include<Windows.h> 
using namespace std;
 
int main()
{
	cout << "type:\t\t" << "---size---" << endl;
	
	cout << "bool:\t\t" << "所占字节数:" << sizeof(bool)
		<< "\t位数:" << sizeof(bool)*8
		<< "\t\t最小值:" << (numeric_limits<bool>::min)() 
		<< "\t\t最大值:" << (numeric_limits<bool>::max)() << endl << endl;
	
	cout << "char:\t\t" << "所占字节数:" << sizeof(char)
		<< "\t位数:" << sizeof(char) * 8
		<< "\t\t最小值:" << (numeric_limits<char>::min)()
		<< "\t\t最大值:" << (numeric_limits<char>::max)() << endl << endl;
	
	cout << "signed char:\t" << "所占字节数:" << sizeof(signed char)
		<< "\t位数:" << sizeof(signed char) * 8
		<< "\t\t最小值:" << (numeric_limits<signed char>::min)()
		<< "\t\t最大值:" << (numeric_limits<signed char>::max)() << endl << endl;
	
	cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char)
		<< "\t位数:" << sizeof(unsigned char) * 8
		<< "\t\t最小值:" << (numeric_limits<unsigned char>::min)()
		<< "\t\t最大值:" << (numeric_limits<unsigned char>::max)() << endl << endl;
	
	cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t)
		<< "\t位数:" << sizeof(wchar_t) * 8
		<< "\t最小值:" << (numeric_limits<wchar_t>::min)()
		<< "\t\t最大值:" << (numeric_limits<wchar_t>::max)() << endl << endl;
	
	cout << "short: \t\t" << "所占字节数:" << sizeof(short)
		<< "\t位数:" << sizeof(short) * 8
		<< "\t最小值:" << (numeric_limits<short>::min)()
		<< "\t\t最大值:" << (numeric_limits<short>::max)() << endl << endl;
	
	cout << "int: \t\t" << "所占字节数:" << sizeof(int)
		<< "\t位数:" << sizeof(int) * 8
		<< "\t最小值:" << (numeric_limits<int>::min)()
		<< "\t最大值:" << (numeric_limits<int>::max)() << endl << endl;
	
	cout << "unsigned int: \t" << "所占字节数:" << sizeof(unsigned)
		<< "\t位数:" << sizeof(unsigned) * 8
		<< "\t最小值:" << (numeric_limits<unsigned>::min)()
		<< "\t\t最大值:" << (numeric_limits<unsigned>::max)() << endl << endl;
	
	cout << "long: \t\t" << "所占字节数:" << sizeof(long)
		<< "\t位数:" << sizeof(long) * 8
		<< "\t最小值:" << (numeric_limits<long>::min)()
		<< "\t最大值:" << (numeric_limits<long>::max)() << endl << endl;
	
	cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long)
		<< "\t位数:" << sizeof(unsigned long) * 8
		<< "\t最小值:" << (numeric_limits<unsigned long>::min)()
		<< "\t\t最大值:" << (numeric_limits<unsigned long>::max)() << endl << endl;
 
	cout << "float: \t\t" << "所占字节数:" << sizeof(float)
		<< "\t位数:" << sizeof(float) * 8
		<< "\t最小值:" << (numeric_limits<float>::min)() 
		<< "\t最大值:" << (numeric_limits<float>::max)() << endl << endl;
		
	cout << "double: \t" << "所占字节数:" << sizeof(double)
		<< "\t位数:" << sizeof(double) * 8
		<< "\t最小值:" << (numeric_limits<double>::min)()
		<< "\t最大值:" << (numeric_limits<double>::max)() << endl << endl;
	
	cout << "size_t: \t" << "所占字节数:" << sizeof(size_t)
		<< "\t位数:" << sizeof(size_t) * 8
		<< "\t最小值:" << (numeric_limits<size_t>::min)()
		<< "\t\t最大值:" << (numeric_limits<size_t>::max)() << endl << endl;
	
	cout << "string: \t" << "所占字节数:" << sizeof(string)
		<< "\t位数:" << sizeof(string) * 8 << endl;
	
	system("pause");
	return 0;
}

运行结果:

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值