在使用C++的过程中,常见的数据类型有bool、char、unsigned char、wchar_t、short、int、unsigned int、long、unsigned long、float、double、size_t、string等。这些类型的变量创建时,系统会自动分配一定的内存用于存储数据。数据存储是以“字节”(Byte)为单位,数据传输大多是以“位”(bit)为单位,也就是二进制中的0或1,8位就是2的8次方。常见类型的字节数在32位和64位的操作系统中有所区别。
单位换算
- 1B(byte,字节)= 8 bit(位);
- 1KB(Kilobyte,千字节)=1024B= 2^10 B;
- 1MB(Megabyte,兆字节,简称“兆”)=1024KB= 2^20 B;
- 1GB(Gigabyte,吉字节,又称“千兆”)=1024MB= 2^30 B;
- 1TB(Terabyte,太字节)=1024GB= 2^40 B;
最大值和最小值
- 最小值=-2^(位数-1)
- 最大值=2^(位数-1)-1
bool
bool类型中规定0为false,非0为true,如果设置bool变量的值为非0数,都会将其转为1,所以最大值为1。
- 所占字节数:1
- 位数:8
- 最小值:0
- 最大值:1
char
- 所占字节数:1
- 位数:8
- 最小值:€
- 最大值:
unsigned char
- 所占字节数:1
- 位数:8
- 最小值:
- 最大值:
wchar_t
- 所占字节数:2
- 位数:16
- 最小值:0
- 最大值:65535
short
- 所占字节数:2
- 位数:16
- 最小值:-32768
- 最大值:32767
int
- 所占字节数:4
- 位数:32
- 最小值:-2147483648
- 最大值:2147483647
unsigned int
- 所占字节数:4
- 位数:32
- 最小值:0
- 最大值:4294967295
long
- 所占字节数:4
- 位数:32
- 最小值:-2147483648
- 最大值:2147483647
unsigned long
- 所占字节数:4
- 位数:32
- 最小值:0
- 最大值:4294967295
float
- 所占字节数:4
- 位数:32
- 最小值:1.17549e-38
- 最大值:3.40282e+38
double
- 所占字节数:8
- 位数:64
- 最小值:2.22507e-308
- 最大值:1.79769e+308
size_t
- 所占字节数:8
- 位数:64
- 最小值:0
- 最大值:18446744073709551615
string
- 所占字节数:32
- 位数:256
源代码
#include<iostream>
#include<string>
#include <limits>
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;
}
输出结果: