C++ 数据类型的范围及其宏
给定任务是要查找不同数据类型的内存范围,即数据类型可以存储从最小值到最大值范围内的值。在内存范围内,可以存储数据的值。很难记住这么大范围的值,因此 C++ 为表示这些数字提供了宏,可以将这些宏直接分配给变量而无需输入整个数字范围。
示例
‘char’(有符号)是字符数据类型,其范围为 -128 至 +128,宏为最小值,查找 char 数据类型范围值的宏为 CHAR_MIN 和 CHAR_MAX。
同样,我们可以通过这些 MIN 和 MAX 宏找到任何数据类型的范围,例如对于‘int’我们可以使用 INT_MIN 和 INT_MAX。
char 范围
128 至 +128
int 范围
-2147483648 至 +2147483648
示例
演示数据类型宏的 C++ 代码
#include<iostream.h> #include<float.h> #include<limits.h> Using namespace std; int main( ) { cout<< " Range of Char :” << CHAR_MIN<< " to ”<<CHAR_MAX; cout<< " Range of int :” <<INT_MIN<< " to " <<INT_MAX; cout<< " Range of float :” <<FLT_MIN<< " to " <<FLT_MAX; cout<< " Range of double :” <<DBL_MIN<< " to " <<DBL_MAX; cout<< " Range of short char :” <<SCHAR_MIN<< " to " <<SCHAR_MAX; cout<< " Range of Unsigned Char :” << 0 << " to " <<UCHAR_MAX; cout<< " Range of long int :” <<LONG_MIN<< " to " <<LONG_MAX; cout<< " Range of Unsigned int :” << 0 << " to " <<UINT_MAX; cout<< " Range of short int :” <<SHRT_MIN<< " to " <<SHRT_MAX; cout<< " Range of float Negative :” <<-FLT_MIN<< " to " <<-FLT_MAX; cout<< " Range of double negative:” <<-DBL_MIN<< " to " <<-DBL_MAX; Return 0; }
输出
如果我们运行上述代码,它将生成以下输出 -
Range of char: -128 to 127 Range of int: -2147483648 to 2147483648 Range of float: 1.17549e-38 to 3.40282e+38 Range of double: 2.22507e-308 to 1.79769e+308 Range of Short char: -128 to 127 Range of Unsigned Char: 0 to 255 Range of long int: -922337203685477580 to 922337203685477580 Range of Unsigned int: 0 to 42944967295 Range of Short int: -32768 to 32767 Range of float negative: -1.17549e-38 to -3.40282e+38 Range of double negative: 2.22507e-308 to 1.79769e+308
广告