c++ 类型int、long、double、char等的表示范围(最大最小值)

骑猪看日落 2022-07-27 14:57 379阅读 0赞

下面是一段测试代码

  1. #include<iostream>
  2. #include<string>
  3. #include <limits>
  4. using namespace std;
  5. int main()
  6. {
  7. cout << "type: \t\t" << "************size**************"<< endl;
  8. cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
  9. cout << "\t最大值:" << (numeric_limits<bool>::max)();
  10. cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
  11. cout << "char: \t\t" << "所占字节数:" << sizeof(char);
  12. cout << "\t最大值:" << (numeric_limits<char>::max)();
  13. cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
  14. cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
  15. cout << "\t最大值:" << (numeric_limits<signed char>::max)();
  16. cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
  17. cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
  18. cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
  19. cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
  20. cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
  21. cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
  22. cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
  23. cout << "short: \t\t" << "所占字节数:" << sizeof(short);
  24. cout << "\t最大值:" << (numeric_limits<short>::max)();
  25. cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
  26. cout << "int: \t\t" << "所占字节数:" << sizeof(int);
  27. cout << "\t最大值:" << (numeric_limits<int>::max)();
  28. cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
  29. cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
  30. cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
  31. cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
  32. cout << "long: \t\t" << "所占字节数:" << sizeof(long);
  33. cout << "\t最大值:" << (numeric_limits<long>::max)();
  34. cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
  35. cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
  36. cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
  37. cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
  38. cout << "double: \t" << "所占字节数:" << sizeof(double);
  39. cout << "\t最大值:" << (numeric_limits<double>::max)();
  40. cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
  41. cout << "long double: \t" << "所占字节数:" << sizeof(long double);
  42. cout << "\t最大值:" << (numeric_limits<long double>::max)();
  43. cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
  44. cout << "float: \t\t" << "所占字节数:" << sizeof(float);
  45. cout << "\t最大值:" << (numeric_limits<float>::max)();
  46. cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
  47. cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
  48. cout << "\t最大值:" << (numeric_limits<size_t>::max)();
  49. cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
  50. cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
  51. // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
  52. cout << "type: \t\t" << "************size**************"<< endl;
  53. return 0;
  54. }

这是vs2012 的输出

SouthEast

这是GCC version 5.1.0 (tdm-1)的输出

SouthEast 1

SouthEast 2

转自:http://blog.csdn.net/xuexiacm/article/details/8122267

发表评论

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

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

相关阅读