常见数据类型的取值范围

在做一些题目时,常常会对各种数据类型的取值范围感到困惑,这里做一个总结。

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "iostream"
#include "climits"
#include "cfloat"
using namespace std;

int main(int argc, char const *argv[])
{
cout << "Range of all kinds of data types: " << endl;
cout << "char: (" << CHAR_MAX << ", " << CHAR_MIN << ")" << endl;
cout << "short: (" << SHRT_MIN << ", " << SHRT_MAX << ")" << endl;
cout << "int: (" << INT_MIN << ", " << INT_MAX << ")" << endl;
cout << "long: (" << LONG_MIN << ", " << LONG_MAX << ")" << endl;
cout << "float: (" << FLT_MIN << ", " << FLT_MAX << ")" << endl;
cout << "double: (" << DBL_MAX << ", " << DBL_MIN << ")" << endl;
cout << "Bits per BYTE: " << CHAR_BIT << endl;
return 0;
}

运行结果

1
2
3
4
5
6
7
8
Range of all kinds of data types:
char: (127, -128)
short: (-32768, 32767)
int: (-2147483648, 2147483647)
long: (-2147483648, 2147483647)
float: (1.17549e-038, 3.40282e+038)
double: (1.79769e+308, 2.22507e-308)
Bits per BYTE: 8 // 每字节位数为8

值得注意的是,对于本机器和编译器而言,int和long型的数据拥有相同的取值范围。

附录:《climits》符号常量一览表

@“climit”


转载请注明来源:©Tinshine