Various types of int, long, double, char for range (maximum minimum)

Source: Internet
Author: User

[CPP]View Plaincopy
  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" << "occupied bytes:" << sizeof (bool);
  9. cout << "\ t max:" << (numeric_limits<Bool>::max) ();
  10. cout << "\t\t min:" << (numeric_limits<bool>::min) () << Endl;
  11. cout << "char: \t\t" << "occupied bytes:" << sizeof (char);
  12. cout << "\ t max:" << (numeric_limits<Char>::max) ();
  13. cout << "\t\t min:" << (numeric_limits<char>::min) () << Endl;
  14. cout << "signed char: \ t" << "Total bytes:" << sizeof (signed char);
  15. cout << "\ t max:" << (numeric_limits<signed Char>::max) ();
  16. cout << "\t\t min:" << (numeric_limits<signed char>::min) () << Endl;
  17. cout << "unsigned char: \ t" << "Total bytes:" << sizeof (unsigned char);
  18. cout << "\ t max:" << (numeric_limits<unsigned Char>::max) ();
  19. cout << "\t\t min:" << (numeric_limits<unsigned char>::min) () << Endl;
  20. cout << "wchar_t: \ T" << "Total bytes:" << sizeof (wchar_t);
  21. cout << "\ t max:" << (numeric_limits<Wchar_t>::max) ();
  22. cout << "\t\t min:" << (numeric_limits<wchar_t>::min) () << Endl;
  23. cout << "short: \t\t" << "occupied bytes:" << sizeof (short);
  24. cout << "\ t max:" << (numeric_limits<Short>::max) ();
  25. cout << "\t\t min:" << (numeric_limits<short>::min) () << Endl;
  26. cout << "int: \t\t" << "occupied bytes:" << sizeof (int);
  27. cout << "\ t max:" << (numeric_limits<Int>::max) ();
  28. cout << "\ t min:" << (numeric_limits<int>::min) << Endl;
  29. cout << "unsigned: \ T" << "Total bytes:" << sizeof (unsigned);
  30. cout << "\ t max:" << (Numeric_limits<unsigned>::max) ();
  31. cout << "\ t min:" << (Numeric_limits<unsigned>::min) () << Endl;
  32. cout << "long: \t\t" << "occupied bytes:" << sizeof (long);
  33. cout << "\ t max:" << (numeric_limits<Long>::max) ();
  34. cout << "\ t min:" << (numeric_limits<long>::min) << Endl;
  35. cout << "unsigned long: \ t" << "occupied bytes:" << sizeof (unsigned long);
  36. cout << "\ t max:" << (numeric_limits<unsigned Long>::max) ();
  37. cout << "\ t min:" << (numeric_limits<unsigned long>::min) << Endl;
  38. cout << "double: \ T" << "Total bytes:" << sizeof (double);
  39. cout << "\ t max:" << (numeric_limits<Double>::max) ();
  40. cout << "\ t min:" << (numeric_limits<double>::min) << Endl;
  41. cout << "long double: \ t" << "occupied bytes:" << sizeof (long double);
  42. cout << "\ t max:" << (numeric_limits<long Double>::max) ();
  43. cout << "\ t min:" << (numeric_limits<long double>::min) << Endl;
  44. cout << "float: \t\t" << "occupied bytes:" << sizeof (float);
  45. cout << "\ t max:" << (numeric_limits<Float>::max) ();
  46. cout << "\ t min:" << (numeric_limits<float>::min) << Endl;
  47. cout << "size_t: \ T" << "Total bytes:" << sizeof (size_t);
  48. cout << "\ t max:" << (numeric_limits<Size_t>::max) ();
  49. cout << "\ t min:" << (numeric_limits<size_t>::min) << Endl;
  50. cout << "string: \ T" << "Total bytes:" << sizeof (string) << Endl;
  51. //<< "\ t maximum:" << (Numeric_limits<string>::max) () << "\ t min:" << (numeric_limits<  String>::min) () << Endl;
  52. cout << "type: \t\t" << "************size**************" << Endl;
  53. return 0;
  54. }



/* Run result analysis:

The above results have been very clear, a few additional notes:

Concept, Integer: An arithmetic type that represents integers, characters, and Booleans is called an integer (integral type).

About signed and unsigned types: integer int, stort, and Long are signed by default. To get the unsigned type, you must make the type unsigned, such as unsigned long. The unsigned int type can be abbreviated to unsigned, that is, unsigned no other type specifier means unsigned int.

A byte represents eight bits, namely: 1byte = 8 bit;

Int:4byte = 0 bit signed signed range: 2^31-1 ~ -2^31, i.e.: 2147483647 ~ 2147483648 unsigned unsigned range: 2^32-1 ~ 0 i.e.: 4294967295 ~

Long:4 byte = + bit with int type

Double:8 byte = bit range: 1.79769e+308 ~ 2.22507e-308

Long Double:12 byte = 1.18973e+4932 bit range: 3.3621e-4932

Float:4 byte = a bit range: 3.40282e+038 ~ 1.17549e-038

The order of magnitude of int, unsigned, long, unsigned long, and double can only be represented as 1 billion, meaning that they represent no more than 10 decimal digits, that is, all 9-bit integers can be saved. and short can only represent 5 bits;

In addition, for floating point, there is basically no mistake in using a double type. The implicit precision loss in float type can not be neglected, and the cost of two-double precision calculation is negligible relative to the single precision. In fact, on some machines, the double type is much faster than the float type calculation. The float type can only guarantee 6 digits, while the double type guarantees at least 15 significant digits (digits after the decimal point), and the precision provided by the long double type is usually unnecessary and bears additional operating costs.

Double is a total of 8 bytes of 64 bits, of which the decimal place occupies 52 bits, 2-^52=2.2204460492503130808472633361816e-16, magnitude of 10^-16, it can guarantee all the accuracy of 2^-15.

On some machines, the run-time price of a long type is much higher than the cost of the same calculation with the int type, so it is necessary to understand the details of the program before calculating the type and compare the actual run-time performance cost of the long type with the int type.

Various types of int, long, double, char for range (maximum minimum)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.