c++基本数据类型的大小及范围

1 数据大小

注意,c语言中没有bool类型,用0代表false,用非0代表true

测试平台:VS2015的debug模式下X86

#include 
using namespace std;

int main(int argc, char* argv[]) {
	cout << "sizeof(bool) = " << sizeof(bool) << endl;
	cout << "sizeof(char) = " << sizeof(char) << endl;
	cout << "sizeof(unsigned char) = " << sizeof(unsigned char) << endl;
	cout << "sizeof(short int) = " << sizeof(short int) << endl;
	cout << "sizeof(int) = " << sizeof(int) << endl;
	cout << "sizeof(unsigned int) = " << sizeof(unsigned int) << endl;
	cout << "sizeof(float) = " << sizeof(float) << endl;
	cout << "sizeof(long) = " << sizeof(long) << endl;
	cout << "sizeof(unsigned long) = " << sizeof(unsigned long) << endl;
	cout << "sizeof(double) = " << sizeof(double) << endl;
	cout << "sizeof(long long) = " << sizeof(long long) << endl;

	cout << "指针" << endl;

	cout << "sizeof(bool*) = " << sizeof(bool*) << endl;
	cout << "sizeof(char*) = " << sizeof(char*) << endl;
	cout << "sizeof(unsigned char*) = " << sizeof(unsigned char*) << endl;
	cout << "sizeof(short int*) = " << sizeof(short int*) << endl;
	cout << "sizeof(int*) = " << sizeof(int*) << endl;
	cout << "sizeof(unsigned int*) = " << sizeof(unsigned int*) << endl;
	cout << "sizeof(float*) = " << sizeof(float*) << endl;
	cout << "sizeof(long*) = " << sizeof(long*) << endl;
	cout << "sizeof(unsigned long*) = " << sizeof(unsigned long*) << endl;
	cout << "sizeof(double*) = " << sizeof(double*) << endl;
	cout << "sizeof(long long*) = " << sizeof(long*) << endl;

	system("pause");
	return 0;
}

运行如下:

c++基本数据类型的大小及范围_第1张图片

总结如下:

数据类型 32位
bool 1
char 1
unsigned char 1
short int 2
int 4
指针 4
unsigned int 4
float 4
long 4
unsigned long 4
double 8
long long 8
指针 4

测试平台:VS2015的debug模式下X64

代码同上

c++基本数据类型的大小及范围_第2张图片

总结

数据类型 64位
bool 1
char 1
unsigned char 1
short int 2
int 4
指针 8
unsigned int 4
float 4
long 4
unsigned long 4
double 8
long long 8

这里有个怪异的地方 ,我们平常说long是8个字节,这里却是4个字节。

我决定试试linux平台,看看结果。

先看32位

在g++后面添加-m32

g++ -m32 -自己的代码

需要安装一些库

sudo apt-get install build-essential module-assistant  
sudo apt-get install gcc-multilib g++-multilib  

结果

c++基本数据类型的大小及范围_第3张图片

 与windows下32位一样

再测64位

c++基本数据类型的大小及范围_第4张图片 发现这里long和unsigned long都是8个字节,其他没有变化。

 总结

数据类型 32位 64位(windows) 64位(linux)
bool 1 1 1
char 1 1 1
unsigned char 1 1 1
short int 2 2 2
int 4 4 4
指针 4 8 8
unsigned int 4 4 4
float 4 4 4
long 4 4 8
unsigned long 4 4 8
double 8 8 8
long long 8 8 8

windows系统和linux系统下,大部分数据类型的大小都一样,只有long和unsigned long不一样;

在window下64位机器上,long和unsigned long都是4个字节

在linux下64位机器人上,long和unsigned long都是8个字节

2 范围

数据类型 占内存字节数 表示范围 数量级
char(signed char) 1 -128~127 2
unsigned char 1 0~255 2
short int(signed short int) 2 -32,768~32,767 4
unsigned short int 2 0~65,535 4
int(signed int) 4 -2,147,483,648~2,147,483,647(-231 ~ 231-1) 9
unsigned int 4 0~4,294,967,295 9
long int(signed long int) 4 -2,147,483,648~2,147,483,647(-231 ~ 231-1) 9
unsigned long int 4 0~4,294,967,295 9
float 4 -3.4x10-38 ~ 3.4x1038 38
double 8 -1.7x10-308 ~ 1.7x10308 308
long double 8 -1.7x10-308 ~ 1.7x10308 308

参考:

1、ubuntu G++编译32位_houxy12的博客-CSDN博客_g++ 编译32位

2、(1条消息) c语言基本数据类型大小(32位/64位操作系统)_华科⁢⁢⁡⁢的博客-CSDN博客_c语言变量类型64位

你可能感兴趣的:(C&C++记录学习,c++,蓝桥杯,c语言)