C++中的数据类型范围及其宏详细指南

【C++中的数据类型范围及其宏详细指南】大多数时候, 在竞争性编程中, 需要分配变量, 数据类型可以容纳的最大值或最小值, 但是记住这么大而精确的数字却是一件困难的工作。因此, C ++具有某些宏来表示这些数字, 因此可以将它们直接分配给变量, 而无需实际键入整数。下面列出其中一些列表。

Data TypeRangeMacro for min valueMacro for max valuechar-128 to +127CHAR_MINCHAR_MAXshort char-128 to +127SCHAR_MINSCHAR_MAXunsigned char0to 2550UCHAR_MAXshort int-32768 to +32767SHRT_MINSHRT_MAXunsigned short int0to655350USHRT_MAX int-2147483648 to +2147483647INT_MININT_MAXunsigned int0to42949672950UINT_MAXlong int-9223372036854775808 to +9223372036854775807LONG_MINLONG_MAXunsigned long int0to184467440737095516150ULONG_MAX long long int-9223372036854775808 to +9223372036854775807LLONG_MINLLONG_MAXunsigned long long int0to184467440737095516150ULLONG_MAXfloat1.17549e-38 to3.40282e+38FLT_MINFLT_MAXfloat(negative)-1.17549e-38 to -3.40282e+38-FLT_MIN-FLT_MAXdouble2.22507e-308 to1.79769e+308DBL_MINDBL_MAXdouble(negative)-2.22507e-308 to -1.79769e+308-DBL_MIN-DBL_MAX

// C++ code to demonstrate the macros for data types #include< iostream> #include< limits.h> // for int, char macros #include< float.h> // for float, double macros using namespace std; int main() {// Displaying ranges with the help of macros cout < < "char ranges from : " < < CHAR_MIN < < " to " < < CHAR_MAX; cout < < "\n\nshort char ranges from : " < < SCHAR_MIN < < " to " < < SCHAR_MAX; cout < < "\n\nunsigned char ranges from : " < < 0 < < " to " < < UCHAR_MAX; cout < < "\n\n\nshort int ranges from : " < < SHRT_MIN < < " to " < < SHRT_MAX; cout < < "\n\nunsigned short int ranges from : " < < 0 < < " to " < < USHRT_MAX; cout < < "\n\nint ranges from : " < < INT_MIN < < " to " < < INT_MAX; cout < < "\n\nunsigned int ranges from : " < < 0 < < " to " < < UINT_MAX; cout < < "\n\nlong int ranges from : " < < LONG_MIN < < " to " < < LONG_MAX; cout < < "\n\nunsigned long int ranges from : " < < 0 < < " to " < < ULONG_MAX; cout < < "\n\nlong long int ranges from : " < < LLONG_MIN < < " to " < < LLONG_MAX; cout < < "\n\nunsigned long long int ranges from : " < < 0 < < " to " < < ULLONG_MAX; cout < < "\n\n\nfloat ranges from : " < < FLT_MIN < < " to " < < FLT_MAX; cout < < "\n\nnegative float ranges from : " < < -FLT_MIN < < " to " < < -FLT_MAX; cout < < "\n\ndouble ranges from : " < < DBL_MIN < < " to " < < DBL_MAX; cout < < "\n\nnegative double ranges from : " < < -DBL_MIN < < " to " < < +DBL_MAX; return 0; }

输出如下:
char ranges from : -128 to 127short char ranges from : -128 to 127unsigned char ranges from : 0 to 255short int ranges from : -32768 to 32767unsigned short int ranges from : 0 to 65535int ranges from : -2147483648 to 2147483647unsigned int ranges from : 0 to 4294967295long int ranges from : -9223372036854775808 to 9223372036854775807unsigned long int ranges from : 0 to 18446744073709551615long long int ranges from : -9223372036854775808 to 9223372036854775807unsigned long long int ranges from : 0 to 18446744073709551615float ranges from : 1.17549e-38 to 3.40282e+38negative float ranges from : -1.17549e-38 to -3.40282e+38double ranges from : 2.22507e-308 to 1.79769e+308negative double ranges from : -2.22507e-308 to 1.79769e+308

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

    推荐阅读