C++基本数据类型的字节数与范围大小

C++基本数据类型的字节数与范围大小,第1张

    C++有几种基本的数据类型:char、int、float、double,这些数据类型的字节数、范围大小根据 *** 作系统、编译器的不同而不同。

图(1) C++基本数据类型的字节数

  • 在Windows上,同一种基本数据类型,其win32与win64的字节数是一致的;比如,int在win32、win64都是4个字节。
  • 在Linux上,大部分基本数据类型,其32位与64位的字节数是一致的;只有long类型的不相同(当然,long long除外,long long在32位、64位的Linux上,都是8字节)。比如,float在32位、64位的Linux上,都是4个字节;double在32位、64位的Linux上都是8个字节。

    当前,在Windows,有些复合数据类型,在不同位数上,其字节数是不同的。比如,string、size_t这类复合数据类型,string在win32上占28个字节,而在win64位上占40个字节。size_t在win32上占4个字节,而在win64位上占8个字节。

1、Windows系统 1.1 32位的编译器
32位 Windows上的数据类型与范围
type:           ************size**************
bool:           所占字节数:1   最大值:1               最小值:0
char:           所占字节数:1   最大值:DEL             最小值:€
signed char:    所占字节数:1   最大值:DEL             最小值:€
unsigned char:  所占字节数:1   最大值:255           最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
short:          所占字节数:2   最大值:32767           最小值:-32768
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**************
1.2 64位的编译器
64位 Windows上的数据类型与范围

type:           ************size**************
bool:           所占字节数:1   最大值:1               最小值:0
char:           所占字节数:1   最大值:DEL             最小值:€
signed char:    所占字节数:1   最大值:DEL             最小值:€
unsigned char:  所占字节数:1   最大值:255             最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
short:          所占字节数:2   最大值:32767           最小值:-32768
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**************

    Windows获取数据类型的字节数、范围大小的案例程序,如下:
    //getSize.cpp

#include  
#include 

using namespace std;

int main()
{
    cout << "type: \t\t" << "************size**************" << endl;
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
    cout << "\t最大值:" << (numeric_limits<bool>::max)();
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << 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;
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);
    cout << "\t最大值:" << (numeric_limits<short>::max)();
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << 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;
    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;
    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;
    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::max)() << "\t最小值:" << (numeric_limits::min)() << endl;  
    cout << "type: \t\t" << "************size**************" << endl;

    system("pause");
    return 0;
}
2、Linux系统 2.1 32位编译器
32位 Linux上的数据类型与范围
type:           ************size**************
bool:           所占字节数:1   最大值:1               最小值:0
char:           所占字节数:1   最大值:Del            最小值:€
signed char:    所占字节数:1   最大值:Del            最小值:€
unsigned char:  所占字节数:1   最大值:255             最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
short:          所占字节数:2   最大值:32767           最小值:-32768
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
2.2 64位编译器
64位 Linux上的数据类型与范围

type:           ************size**************
bool:           所占字节数:1   最大值:1                     最小值:0
char:           所占字节数:1   最大值:DEL                  最小值:€
signed char:    所占字节数:1   最大值:DEL                  最小值:€
unsigned char:  所占字节数:1   最大值:255                   最小值:0
wchar_t:        所占字节数:2   最大值:65535                 最小值:0
short:          所占字节数:2   最大值:32767                 最小值:-32768
int:            所占字节数:4   最大值:2147483647            最小值:-2147483648
unsigned:       所占字节数:4   最大值:4294967295            最小值:0
long:           所占字节数:8   最大值:9223372036854775807   最小值:-9223372036854775807
unsigned long:  所占字节数:8   最大值:18446744073709551615  最小值: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**************

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/914997.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存