C++ 中的数据类型范围及其宏¶
在竞争性编程中,通常需要将变量赋值为数据类型所能容纳的最大值或最小值。然而,记住这些大而精确的数字是一项困难的任务。因此,C++ 提供了一些宏来表示这些数字,这样可以直接将它们赋值给变量,而不必实际输入整个数字。以下是一些宏的列表:
数据类型 | 范围 | 宏的最小值 | 宏的最大值 |
---|---|---|---|
char | -128 to +127 | CHAR_MIN | CHAR_MAX |
short char | -128 to +127 | SCHAR_MIN | SCHAR_MAX |
unsigned char | 0 to 255 | — | UCHAR_MAX |
short int | -32768 to +32767 | SHRT_MIN | SHRT_MAX |
unsigned short int | 0 to 65535 | — | USHRT_MAX |
int | -2147483648 to +2147483647 | INT_MIN | INT_MAX |
unsigned int | 0 to 4294967295 | — | UINT_MAX |
long int | -9223372036854775808 to +9223372036854775807 | LONG_MIN | LONG_MAX |
unsigned long int | 0 to 18446744073709551615 | — | ULONG_MAX |
long long int | -9223372036854775808 to +9223372036854775807 | LLONG_MIN | LLONG_MAX |
unsigned long long int | 0 to 18446744073709551615 | — | ULLONG_MAX |
float | 1.17549e-38 to 3.40282e+38 | FLT_MIN | FLT_MAX |
float (negative) | -1.17549e-38 to -3.40282e+38 | -FLT_MIN | -FLT_MAX |
double | 2.22507e-308 to 1.79769e+308 | DBL_MIN | DBL_MAX |
double (negative) | -2.22507e-308 to -1.79769e+308 | -DBL_MIN | -DBL_MAX |
样例
以下示例展示了数据类型的宏。
// 演示数据类型宏的 C++ 代码
#include <float.h> // 对于浮点数,双精度浮点数的宏
#include <iostream>
#include <limits.h> // 对于整型,字符型宏
using namespace std;
int main()
{
//使用宏显示数据类型范围
cout << "char ranges from: " << CHAR_MIN << " to "
<< CHAR_MAX << endl;
cout << "\nshort char ranges from: " << SCHAR_MIN
<< " to " << SCHAR_MAX << endl;
cout << "\nunsigned char ranges from: " << 0 << " to "
<< UCHAR_MAX << endl;
cout << "\n\nshort int ranges from: " << SHRT_MIN
<< " to " << SHRT_MAX << endl;
cout << "\nunsigned short int ranges from: " << 0
<< " to " << USHRT_MAX << endl;
cout << "\nint ranges from: " << INT_MIN << " to "
<< INT_MAX << endl;
cout << "\nunsigned int ranges from: " << 0 << " to "
<< UINT_MAX << endl;
cout << "\nlong int ranges from: " << LONG_MIN << " to "
<< LONG_MAX << endl;
cout << "\nunsigned long int ranges from: " << 0
<< " to " << ULONG_MAX << endl;
cout << "\nlong long int ranges from: " << LLONG_MIN
<< " to " << LLONG_MAX << endl;
cout << "\nunsigned long long int ranges from: " << 0
<< " to " << ULLONG_MAX << endl;
cout << "\n\nfloat ranges from: " << FLT_MIN << " to "
<< FLT_MAX << endl;
cout << "\nnegative float ranges from: " << -FLT_MIN
<< " to " << -FLT_MAX << endl;
cout << "\ndouble ranges from: " << DBL_MIN << " to "
<< DBL_MAX << endl;
cout << "\nnegative double ranges from: " << -DBL_MIN
<< " to " << -DBL_MAX << endl;
return 0;
}
输出
char ranges from: -128 to 127
short char ranges from: -128 to 127
unsigned char ranges from: 0 to 255
short int ranges from: -32768 to 32767
unsigned short int ranges from: 0 to 65535
int ranges from: -2147483648 to 2147483647
unsigned int ranges from: 0 to 4294967295
long int ranges from: -9223372036854775808 to 9223372036854775807
unsigned long int ranges from: 0 to 18446744073709551615
long long int ranges from: -9223372036854775808 to 9223372036854775807
unsigned long long int ranges from: 0 to 18446744073709551615
float ranges from: 1.17549e-38 to 3.40282e+38
negative float ranges from: -1.17549e-38 to -3.40282e+38
double ranges from: 2.22507e-308 to 1.79769e+308
negative double ranges from: -2.22507e-308 to -1.79769e+308