[C++]cpp小笔记3 --- C++基础类型的大小和转换
- 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
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
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.
- 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);