C++数据类型占字节大小分析

概述

C++基本数据类型

类型关键字
布尔型bool
字符型char
整型int
浮点型float
双浮点型double
无类型void
宽字符型wchar_t

一些基本类型可以使用一个或多个类型修饰符进行修饰:

  • signed

  • unsigned

  • short

  • long

各数据类型在内存中所占字节的大小随系统的差异而变,可通过sizeof()函数查看,下面直接总结一些基本类型所占字节的大小。

所占字节大小总结

类型32位系统64位系统
bool11
char11
short22
int44
long44
double88
float44
*48

注:有的地方说64位系统中long占8字节,不过在我的电脑中实测确是4字节,所以还是要根据实际的硬件及编译器确定到底占用多少!

测试程序

几点说明:

  • signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int

  • 但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。
    如果用于文本,则使用未加限定的char, 是类似于 'a', '0'的类型, 或是组成C字符串"abcde"的类型。它也可以是一个值,但是是当做无符号还是有符号数没有指定。
    如果用于数字,那么: signed char, 范围 -128 到 127,unsigned char, 范围 0 ~ 255。

  • signed与signed int与int是等价类型

#include<iostream>  
#include<string>  
#include <limits>  
using namespace std;

int main()
{
	/*
	signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
	但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。使用char的时候最好标明是signed char还是unsigned char。
	signed与signed int与int是等价类型
	*/
	cout << "type: \t\t" << "************size**************" << endl;
	// *
	cout << "--------------------------------" << endl;
	int *p;
	cout << "*p: \t\t" << "所占字节数:" << sizeof(p) << endl;

	// bool
	cout << "--------------------------------" << endl;
	cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
	cout << "\t最大值:" << (numeric_limits<bool>::max)();
	cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;

	// char
	cout << "--------------------------------" << endl;
	cout << "char: \t\t" << "所占字节数:" << sizeof(char);
	cout << "\t最大值:" << (numeric_limits<char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;

	cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
	cout << "\t最大值:" << (numeric_limits<signed char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;

	cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
	cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;

	// short (typedef short int wchar_t;)
	cout << "--------------------------------" << endl;
	cout << "short: \t\t" << "所占字节数:" << sizeof(short);
	cout << "\t最大值:" << (numeric_limits<short>::max)();
	cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;

	cout << "unsigned short:\t" << "所占字节数:" << sizeof(unsigned short);
	cout << "\t最大值:" << (numeric_limits<unsigned short>::max)();
	cout << "\t\t最小值:" << (numeric_limits<unsigned short>::min)() << endl;

	cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
	cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
	cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;

	// int
	cout << "--------------------------------" << endl;
	cout << "int: \t\t" << "所占字节数:" << sizeof(int);
	cout << "\t最大值:" << (numeric_limits<int>::max)();
	cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;

	cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
	cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;

	// long
	cout << "--------------------------------" << endl;
	cout << "long: \t\t" << "所占字节数:" << sizeof(long);
	cout << "\t最大值:" << (numeric_limits<long>::max)();
	cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;

	cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
	cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;

	// double
	cout << "--------------------------------" << endl;
	cout << "double: \t" << "所占字节数:" << sizeof(double);
	cout << "\t最大值:" << (numeric_limits<double>::max)();
	cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;

	cout << "long double: \t" << "所占字节数:" << sizeof(long double);
	cout << "\t最大值:" << (numeric_limits<long double>::max)();
	cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;

	// float
	cout << "--------------------------------" << endl;
	cout << "float: \t\t" << "所占字节数:" << sizeof(float);
	cout << "\t最大值:" << (numeric_limits<float>::max)();
	cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;

	cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
	cout << "\t最大值:" << (numeric_limits<size_t>::max)();
	cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;

	cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
	// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
	
	cout << "type: \t\t" << "************size**************" << endl;
	
	return 0;
}

64位编译器结果(Visual Studio x64)

type:           ************size**************
--------------------------------
*p:             所占字节数:8
--------------------------------
bool:           所占字节数:1   最大值:1               最小值:0
--------------------------------
char:           所占字节数:1   最大值:                最小值:€
signed char:    所占字节数:1   最大值:                最小值:€
unsigned char:  所占字节数:1   最大值:              最小值:
--------------------------------
short:          所占字节数:2   最大值:32767           最小值:-32768
unsigned short: 所占字节数:2   最大值:65535           最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
--------------------------------
int:            所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned:       所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
long:           所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned long:  所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
double:         所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
long double:    所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
--------------------------------
float:          所占字节数:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所占字节数:8   最大值:18446744073709551615    最小值:0
string:         所占字节数:40
type:           ************size**************

32位编译器结果(Visual Studio x86)

type:           ************size**************
--------------------------------
*p:             所占字节数:4
--------------------------------
bool:           所占字节数:1   最大值:1               最小值:0
--------------------------------
char:           所占字节数:1   最大值:                最小值:€
signed char:    所占字节数:1   最大值:                最小值:€
unsigned char:  所占字节数:1   最大值:              最小值:
--------------------------------
short:          所占字节数:2   最大值:32767           最小值:-32768
unsigned short: 所占字节数:2   最大值:65535           最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
--------------------------------
int:            所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned:       所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
long:           所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned long:  所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
double:         所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
long double:    所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
--------------------------------
float:          所占字节数:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所占字节数:4   最大值:4294967295      最小值:0
string:         所占字节数:28
type:           ************size**************

参考:菜鸟教程C++数据类型

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值