1. 类型的大小
- A short integer is at least 16 bits wide.
- An int integer is at least as big as short.
- A long integer is at least 32 bits wide and at least as big as int.
- A long long integer is at least 64 bits wide and at least as big as long.
2. sizeof and climits
int days = 0
cout << "size of days is " << sizeof(days) << endl;
cout << "size of int is " << sizeof(int) << endl;
Results:
size of days is 4
size of int is 4
climits中定义了c++中基础类型的最大值,最小值,大小的常量值。
CHAR_BIT Number of bits in a char
CHAR_MAX Maximum char value
CHAR_MIN Minimum char value
SCHAR_MAX Maximum signed char value
SCHAR_MIN Minimum signed char value
UCHAR_MAX Maximum unsigned char value
SHRT_MAX Maximum short value
SHRT_MIN Minimum short value
USHRT_MAX Maximum unsigned short value
#include <climits>
// use limits.h for older systems
3. 无符号类型
unsigned short change;
unsigned int rovert;
unsigned quarterback; //unsigned int type
unsigned long gone;
unsigned long long lang_lang;
从这张图上可以看出
If signed integer max value is 32767. 32767 + 1, result is -32768.
If unsigned integer max value is 65535, if 65535 + 1, result is 0.
4. 类型转换。
自动类型转换:
- C++会自动将converts bool, char, unsigned char, signed char, and short values to int.
- The unsigned short type is converted to int if short is smaller than int. If the two types are the same size, unsigned short is con-
verted to unsigned int. This rule ensures that there’s no data loss in promoting unsigned short. - When an operation involves two types, the smaller is converted to the larger.
int a = 0;
double b = (double)a;
OR
double b = double(a);
double b = static_cast<double> (a);