C++ 数据类型范围及其宏
在某些情况下,我们需要在不同的问题中使用特定数据类型的最小值或最大值。记住这些值非常困难。因此,C++ 提供了一些宏,用于表示某些数据类型的最小和最大范围。有些数据类型没有宏,因为它们是无符号的,因此最小值为 0。
数据类型 | 范围 | 最小值宏 | 最大值宏 |
---|---|---|---|
char | -128 到 +127 | CHAR_MIN | CHAR_MAX |
有符号字符型 (signed char) | -128 到 +127 | SCHAR_MIN | SCHAR_MAX |
无符号字符型 (unsigned char) | 0 到 255 | ----- | UCHAR_MAX |
短整型 (short int) | -32768 到 +32767 | SHRT_MIN | SHRT_MAX |
无符号短整型 (unsigned short int) | 0 到 65535 | ----- | USHRT_MAX |
整型 (int) | -2147483648 到 +2147483647 | INT_MIN | INT_MAX |
无符号整型 (unsigned int) | 0 到 4294967295 | ----- | INT_MAX |
长整型 (long int) | -2147483648 到 +2147483647 | LONG_MIN | LONG_MAX |
无符号长整型 (unsigned long int) | 0 到 18446744073709551615 | ----- | ULONG_MAX |
长长整型 (long long int) | -9223372036854775808 到 +9223372036854775807 | LLONG_MIN | LLONG_MAX |
无符号长长整型 (unsigned long long int) | 0 到 18446744073709551615 | ----- | ULLONG_MAX |
单精度浮点型 (float) | 1.17549e-38 到 3.40282e+38 | FLT_MIN | FLT_MAX |
负单精度浮点型 (negative float) | -1.17549e-38 到 -3.40282e+38 | -FLT_MIN | -FLT_MAX |
双精度浮点型 (double) | 2.22507e-308 到 1.79769e+308 | DBL_MIN | DBL_MAX |
负双精度浮点型 (negative double) | -2.22507e-308 到 -1.79769e+308 | -DBL_MIN | -DBL_MAX |
我们可以看到一个简单的程序,用于打印 C++ 中某些数据类型的范围。
示例
#include<iostream> #include<limits.h> // for int,char macros #include<float.h> // for float,double macros using namespace std; int main() { cout << "char Range: (" << CHAR_MIN <<", " <<CHAR_MAX << ")\n"; cout << "short char Range: (" << SCHAR_MIN <<", " <<SCHAR_MAX << ")\n"; cout << "unsigned char Range: (" << 0 <<", " <<UCHAR_MAX << ")\n"; cout << "short int Range: (" << SHRT_MIN <<", " <<SHRT_MAX << ")\n"; cout << "unsigned short int Range: (" << 0 <<", " <<USHRT_MAX << ")\n"; cout << "int Range: (" << INT_MIN <<", " <<INT_MAX << ")\n"; cout << "unsigned int Range: (" << 0 <<", " <<UINT_MAX << ")\n"; cout << "long int Range: (" << LONG_MIN <<", " <<LONG_MAX << ")\n"; cout << "unsigned long int Range: (" << 0 <<", " <<ULONG_MAX << ")\n"; cout << "long long int Range: (" << LLONG_MIN <<", " <<LLONG_MAX << ")\n"; cout << "unsigned long long int Range: (" << 0 <<", " <<ULLONG_MAX << ")\n"; cout << "float Range: (" << FLT_MIN <<", " <<FLT_MAX << ")\n"; cout << "float(negative) Range: (" << -FLT_MIN <<", " <<-FLT_MAX << ")\n"; cout << "double Range: (" << DBL_MIN <<", " <<DBL_MAX << ")\n"; cout << "double(negative) Range: (" << -DBL_MIN <<", " <<-DBL_MAX << ")"; }
输出
char Range: (-128, 127) short char Range: (-128, 127) unsigned char Range: (0, 255) short int Range: (-32768, 32767) unsigned short int Range: (0, 65535) int Range: (-2147483648, 2147483647) unsigned int Range: (0, 4294967295) long int Range: (-2147483648, 2147483647) unsigned long int Range: (0, 4294967295) long long int Range: (-9223372036854775808, 9223372036854775807) unsigned long long int Range: (0, 18446744073709551615) float Range: (1.17549e-038, 3.40282e+038) float(negative) Range: (-1.17549e-038, -3.40282e+038) double Range: (2.22507e-308, 1.79769e+308) double(negative) Range: (-2.22507e-308, -1.79769e+308)
广告