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

1 数据大小

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

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

#include <iostream>
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;
}

运行如下:

总结如下:

数据类型32位
bool1
char1
unsigned char1
short int2
int4
指针4
unsigned int4
float4
long4
unsigned long4
double8
long long8
指针4

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

代码同上

总结

数据类型64位
bool1
char1
unsigned char1
short int2
int4
指针8
unsigned int4
float4
long4
unsigned long4
double8
long long8

这里有个怪异的地方 ,我们平常说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  

结果

 与windows下32位一样

再测64位

 发现这里long和unsigned long都是8个字节,其他没有变化。

 总结

数据类型32位64位(windows)64位(linux)
bool111
char111
unsigned char111
short int222
int444
指针488
unsigned int444
float444
long448
unsigned long448
double888
long long888

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

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

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

2 范围

数据类型占内存字节数表示范围数量级
char(signed char)1-128~1272
unsigned char10~2552
short int(signed short int)2-32,768~32,7674
unsigned short int20~65,5354
int(signed int)4-2,147,483,648~2,147,483,647(-231 ~ 231-1)9
unsigned int40~4,294,967,2959
long int(signed long int)4-2,147,483,648~2,147,483,647(-231 ~ 231-1)9
unsigned long int40~4,294,967,2959
float4-3.4x10-38 ~ 3.4x103838
double8-1.7x10-308 ~ 1.7x10308308
long double8-1.7x10-308 ~ 1.7x10308308

参考:

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值