速查表:
char -128 ~ +127 (1 Byte)
unsigned char 0 ~ 255 (1 Bytes)
short -32767 ~ + 32768 (2 Bytes)
unsigned short 0 ~ 65536 (2 Bytes)
int -2147483648 ~ +2147483647 (4 Bytes)
unsigned int 0 ~ 4294967295 (4 Bytes)
long == int
long long -9223372036854775808 ~ +9223372036854775807 (8 Bytes)
unsigned long long 0 ~ 18446744073709551615 (8 Bytes)
为方便起见,把先把表格放在了开头,接下来是探究标准库各个类型的数据大小,不同编译器也有一定的差别。前提知识:#include这个源文件中有关于类型数据大小的定义.
下面是源代码:
#include<iostream>
#include<climits>
using namespace std;
int main()
{
cout<<"char类型的字节数(byte):"<<sizeof(char)<<"字节"<<endl; // 1byte==8bit
cout<<"char类型的最大值和最小值:"<<CHAR_MAX<<"\t"<<CHAR_MIN<<endl;
cout<<"signed char类型的最大值和最小值:"<<SCHAR_MAX<<"\t"<<SCHAR_MIN<<endl;
cout<<"unsigned char类型的最大值(2^8-1)和最小值:"<<UCHAR_MAX<<"\t"<<0<<endl;
cout<<"short类型的最大值和最小值及字节数:"<<SHRT_MAX<<"\t"<<SHRT_MIN<<"\t"<<sizeof(short)<<"字节"<<endl;
cout<<"unsigned short类型的最大值和最小值及字节数:"<<USHRT_MAX<<"\t"<<0<<endl;
cout<<"int类型的最大值和最小值及字节数:"<<INT_MAX<<"\t"<<INT_MIN<<"\t"<<sizeof(int)<<"字节"<<endl;
cout<<"unsigned int类型的最大值和最小值:"<<UINT_MAX<<"\t"<<0<<endl;
cout<<"long类型的最大值和最小值及字节数:"<<LONG_MAX<<"\t"<<LONG_MIN<<'\t'<<sizeof(long)<<"字节"<<endl;
cout<<"unsigned long类型的最大值和最小值:"<<ULONG_MAX<<"\t"<<0<<endl;
cout<<"long long类型的最大值和最小值及字节数:"<<LLONG_MAX<<"\t"<<LLONG_MIN<<"\t"<<sizeof(long long)<<"字节"<<endl;
cout<<"unsigned long long类型的最大值和最小值:"<<ULLONG_MAX<<"\t"<<0<<endl;
cout<<3.4E+38;
}
眼见为实,编译结果如图:
这是笔者计算机上的运行结果图。
直接从结果来看,看来上面的数据表格是没有问题的。