In C/C ++, int, long, double, and char Types indicate the range (maximum and minimum values) and doublechar
[Cpp]View plaincopy
- # Include <iostream>
- # Include <string>
- # Include <limits>
- Using namespace std;
- Int main ()
- {
- Cout <"type: \ t "<" ************* size *************** "<endl;
- Cout <"bool: \ t" <"Bytes:" <sizeof (bool );
- Cout <"\ t maximum value:" <(numeric_limits <bool >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <bool>: min) () <endl;
- Cout <"char: \ t" <"Bytes:" <sizeof (char );
- Cout <"\ t maximum value:" <(numeric_limits <char>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <char >:: min) () <endl;
- Cout <"signed char: \ t" <"Bytes:" <sizeof (signed char );
- Cout <"\ t maximum value:" <(numeric_limits <signed char >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <signed char>: min) () <endl;
- Cout <"unsigned char: \ t" <"Bytes:" <sizeof (unsigned char );
- Cout <"\ t maximum value:" <(numeric_limits <unsigned char >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <unsigned char>: min) () <endl;
- Cout <"wchar_t: \ t" <"Bytes:" <sizeof (wchar_t );
- Cout <"\ t maximum value:" <(numeric_limits <wchar_t>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <wchar_t>: min) () <endl;
- Cout <"short: \ t" <"Bytes:" <sizeof (short );
- Cout <"\ t maximum value:" <(numeric_limits <short >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <short>: min) () <endl;
- Cout <"int: \ t" <"Bytes:" <sizeof (int );
- Cout <"\ t maximum value:" <(numeric_limits <int>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <int>: min) () <endl;
- Cout <"unsigned: \ t" <"Bytes:" <sizeof (unsigned );
- Cout <"\ t maximum value:" <(numeric_limits <unsigned>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <unsigned>: min) () <endl;
- Cout <"long: \ t" <"Bytes:" <sizeof (long );
- Cout <"\ t maximum value:" <(numeric_limits <long>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <long>: min) () <endl;
- Cout <"unsigned long: \ t" <"Bytes:" <sizeof (unsigned long );
- Cout <"\ t maximum value:" <(numeric_limits <unsigned long>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <unsigned long>: min) () <endl;
- Cout <"double: \ t" <"Bytes:" <sizeof (double );
- Cout <"\ t maximum value:" <(numeric_limits <double >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <double >:: min) () <endl;
- Cout <"long double: \ t" <"Bytes:" <sizeof (long double );
- Cout <"\ t maximum value:" <(numeric_limits <long double >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <long double >:: min) () <endl;
- Cout <"float: \ t" <"Bytes:" <sizeof (float );
- Cout <"\ t maximum value:" <(numeric_limits <float >:: max )();
- Cout <"\ t minimum value:" <(numeric_limits <float>: min) () <endl;
- Cout <"size_t: \ t" <"number of bytes:" <sizeof (size_t );
- Cout <"\ t maximum value:" <(numeric_limits <size_t>: max )();
- Cout <"\ t minimum value:" <(numeric_limits <size_t>: min) () <endl;
- Cout <"string: \ t" <"Bytes:" <sizeof (string) <endl;
- // <"\ T maximum value:" <(numeric_limits <string>: max) () <"\ t minimum value:" <(numeric_limits <string> :: min) () <endl;
- Cout <"type: \ t "<" ************* size *************** "<endl;
- Return 0;
- }
/* Running result analysis:
The above results are quite clear. Here are some additional notes:
Concept and integer: The arithmetic types that indicate integers, characters, and boolean values are collectively called integer types ).
About the signed and unsigned types: integer int, stort, and long are both signed by default. To obtain the unsigned type, you must specify this type as unsigned, for example, unsigned long. The unsigned int type can be abbreviated as unsigned. That is to say, the unsigned int type is unsigned without any other type specifiers.
One byte indicates eight bits, that is, 1 byte = 8 bits;
Int: 4 byte = 32 bit signed range: 2 ^ 31-1 ~ -2 ^ 31: 2147483647 ~ -2147483648 unsigned range: 2 ^ 32-1 ~ 0 means: 4294967295 ~ 0
Long: 4 byte = 32 bit same as int type
Double: 8 byte = 64 bit range: 1.79769e + 308 ~ 2.22 507e-308
Long double: 12 byte = 96 bit; value range: 1.18973e + 4932 ~ 3.3621 e-4932
Float: 4 byte = 32 bit range: 3.40282e + 038 ~ 1.17549e-038
The maximum values of int, unsigned, long, unsigned long, and double values can only be expressed as 1 billion. That is, they indicate no more than 10 decimal digits, that is, all 9-digit integers can be saved. Short can only represent five digits;
In addition, for floating point, there is basically no error in using the double type. In the float type, the implicit loss of precision cannot be ignored, and the cost of the second double precision calculation can be ignored relative to the single precision. In fact, on some machines, the double type is much faster than the float type. The float type can only ensure 6 Valid digits, while the double type can ensure at least 15 valid digits (digits after the decimal point). The precision provided by the long double type is usually unnecessary, it also bears additional operation costs.
Double is an 8-byte 64-bit, where decimal places occupy 52 digits, 2-^ 52 = 2.2204460213503130808472633361816e-16, and the magnitude is 10 ^-16, therefore, it can ensure all the precision of 2 ^-15.
On some machines, the running price of long TYPE calculation is much higher than that of int type calculation, therefore, before calculating the type, you must first understand the program details and compare the actual runtime performance cost of the long and int types.