C、C++数据类型(int, long, short, char, long long) 的取值范围、最大最小值:climits 里的一些宏

- 日理万妓 2022-07-20 12:10 275阅读 0赞
  1. #include <iostream>
  2. #include <climits>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "int is " <<sizeof(int) << " bytes." << endl;
  7. cout << "short is " <<sizeof(short) << " bytes." << endl;
  8. cout << "long is " <<sizeof(long) << " bytes." << endl;
  9. cout << "long long is " <<sizeof(long long) << " bytes." << endl;
  10. cout << endl;
  11. cout << "*****Maximum values:*****" << endl;
  12. cout << "int: " << INT_MAX << endl;
  13. cout << "short: " << SHRT_MAX << endl;
  14. cout << "long: " << LONG_MAX << endl;
  15. cout << "long long: " << LLONG_MAX << endl;
  16. cout << "long long: " << LONG_LONG_MAX << endl;//和上一个相同
  17. cout << "char: " << CHAR_MAX << endl;
  18. cout << "signed char: " << SCHAR_MAX << endl;
  19. cout << "unsigned char: " << CHAR_MAX << endl;
  20. cout << "unsigned short: " << USHRT_MAX << endl;
  21. // cout << "unsigned int: " << UNIT_MAX << endl;//报错
  22. cout << "unsigned long: " << ULONG_MAX << endl;
  23. cout << "unsigned long long: " << ULLONG_MAX << endl;
  24. cout << "unsigned long long: " << ULONG_LONG_MAX << endl;//和上一个相同
  25. cout << endl;
  26. cout << "*****Minimum values:*****" << endl;
  27. cout << "int: " << INT_MIN << endl;
  28. cout << "short: " << SHRT_MIN << endl;
  29. cout << "long: " << LONG_MIN << endl;
  30. cout << "long long: " << LLONG_MIN << endl;
  31. cout << "long long: " << LONG_LONG_MIN << endl;//和上一个相同
  32. cout << "char: " << CHAR_MIN << endl;
  33. cout << "signed char: " << SCHAR_MIN << endl;
  34. cout << endl;
  35. cout<<"Bits per byte = "<<CHAR_BIT<<endl;
  36. return 0;
  37. }
  38. 64win7运行结果
  39. int is 4 bytes.
  40. short is 2 bytes.
  41. long is 4 bytes.
  42. long long is 8 bytes.
  43. *****Maximum values:*****
  44. int: 2147483647
  45. short: 32767
  46. long: 2147483647
  47. long long: 9223372036854775807
  48. long long: 9223372036854775807
  49. char: 127
  50. signed char: 127
  51. unsigned char: 127
  52. unsigned short: 65535
  53. unsigned long: 4294967295
  54. unsigned long long: 18446744073709551615
  55. unsigned long long: 18446744073709551615
  56. *****Minimum values:*****
  57. int: -2147483648
  58. short: -32768
  59. long: -2147483648
  60. long long: -9223372036854775808
  61. long long: -9223372036854775808
  62. char: -128
  63. signed char: -128
  64. Bits per byte = 8
  65. #include<iostream>
  66. #include<string>
  67. #include <limits>
  68. using namespace std;
  69. int main()
  70. {
  71. cout << "type: \t\t" << "************size**************"<< endl;
  72. cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
  73. cout << "\t最大值:" << (numeric_limits<bool>::max)();
  74. cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
  75. cout << "char: \t\t" << "所占字节数:" << sizeof(char);
  76. cout << "\t最大值:" << (numeric_limits<char>::max)();
  77. cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
  78. cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
  79. cout << "\t最大值:" << (numeric_limits<signed char>::max)();
  80. cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
  81. cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
  82. cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
  83. cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
  84. cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
  85. cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
  86. cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
  87. cout << "short: \t\t" << "所占字节数:" << sizeof(short);
  88. cout << "\t最大值:" << (numeric_limits<short>::max)();
  89. cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
  90. cout << "int: \t\t" << "所占字节数:" << sizeof(int);
  91. cout << "\t最大值:" << (numeric_limits<int>::max)();
  92. cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
  93. cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
  94. cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
  95. cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
  96. cout << "long: \t\t" << "所占字节数:" << sizeof(long);
  97. cout << "\t最大值:" << (numeric_limits<long>::max)();
  98. cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
  99. cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
  100. cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
  101. cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
  102. cout << "double: \t" << "所占字节数:" << sizeof(double);
  103. cout << "\t最大值:" << (numeric_limits<double>::max)();
  104. cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
  105. cout << "long double: \t" << "所占字节数:" << sizeof(long double);
  106. cout << "\t最大值:" << (numeric_limits<long double>::max)();
  107. cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
  108. cout << "float: \t\t" << "所占字节数:" << sizeof(float);
  109. cout << "\t最大值:" << (numeric_limits<float>::max)();
  110. cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
  111. cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
  112. cout << "\t最大值:" << (numeric_limits<size_t>::max)();
  113. cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
  114. cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
  115. // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
  116. cout << "type: \t\t" << "************size**************"<< endl;
  117. return 0;
  118. }
  119. 64win7运行结果
  120. type: *****************************SIZE****************************
  121. bool: 所占字节数:1 最大值:1 最小值:0
  122. char: 所占字节数:1 最大值: 最小值:€
  123. signed char: 所占字节数:1 最大值: 最小值:€
  124. unsigned char: 所占字节数:1 最大值: 最小值:
  125. wchar_t: 所占字节数:2 最大值:65535 最小值:0
  126. short: 所占字节数:2 最大值:32767 最小值:-32768
  127. int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
  128. unsigned: 所占字节数:4 最大值:4294967295 最小值:0
  129. long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
  130. unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
  131. double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
  132. long double: 所占字节数:12 最大值:Infinity 最小值:0
  133. float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
  134. size_t: 所占字节数:4 最大值:4294967295 最小值:0
  135. string: 所占字节数:4
  136. type: *****************************SIZE****************************

/*运行结果分析:

以上结果已经很明白了,一下补充说明几点:

概念、整型:表示整数、字符和布尔值的算术类型合称为整型(integral type)。

关于带符号与无符号类型:整型 int、stort 和 long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。

一字节表示八位,即:1byte = 8 bit;

int: 4byte = 32 bit有符号signed范围:2^31-1 ~ -2^31即:2147483647 ~ -2147483648无符号unsigned范围:2^32-1 ~ 0即:4294967295 ~ 0

long: 4 byte = 32 bit同int型

double: 8 byte = 64 bit范围:1.79769e+308 ~ 2.22507e-308

long double: 12 byte = 96 bit范围: 1.18973e+4932 ~ 3.3621e-4932

float: 4 byte = 32 bit范围: 3.40282e+038 ~ 1.17549e-038

int、unsigned、long、unsigned long 、double的数量级最大都只能表示为10亿,即它们表示十进制的位数不超过10个,即可以保存所有9位整数。而short只是能表示5位;

另外对于浮点说而言:使用double类型基本上不会有错。在float类型中隐式的精度损失是不能忽视的,二双精度计算的代价相对于单精度可以忽略。事实上,在有些机器上,double类型比float类型的计算要快得多。float型只能保证6位有效数字,而double型至少可以保证15位有效数字(小数点后的数位),long double型提供的精度通常没有必要,而且还要承担额外的运行代价。

double是8字节共64位,其中小数位占52位,2-^52=2.2204460492503130808472633361816e-16,量级为10^-16,故能够保证2^-15的所有精度。

在有些机器上,用long类型进行计算所付出的运行时代价远远高于用int类型进行同样计算的代价,所以算则类型前要先了解程序的细节并且比较long类型与int类型的实际运行时性能代价。

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读