对于整数来说,它有有符号和无符号之分;对于实数,无论是单精度还是双精度,都是有符号的(有正有负);
- 布尔类型;只有两值,无所谓有无符号;true|false, 1种;
- 字符类型;包括有无符号;signed(可以省略),2种;
- 短整型;包括有无符号;2种
- 整型;有无符号,2种;
- 长整型;有无符号,2种;
- 长长整型;有无符号,2种;
- 单精度;有符号,1种;
- 双精度;有符号,1种;
共计13种
2.长度
在内存分配时,无论静态还是动态,占几个字节的存储空间;用运算符sizeof获取;使用时:sizeof 变量|数据类型|字面常量; sizeof(变量|数据类型|字面常量)
范围
因为在内存中的数据是分段的,每种数据类型都有范围的,分为最小值和最大值;
下面我们写代码将所有基本类型的长度及范围打印出来,引入一个新的头文件:climits
代码示例:
#include#include using namespace std; int main() { //.............................................................. cout << " ~~布尔类型~~" << endl; cout << " 它占: " << sizeof(bool) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~有符号字符类型~~" << endl; cout << " 它占: " << sizeof(char) << " 个字节!" << endl; cout << " 最小值:" << (int)(numeric_limits ::min()) << endl; cout << " 最大值:" << (int)(numeric_limits ::max()) << endl; cout << endl; cout << " ~~无符号字符类型~~" << endl; cout << " 它占: " << sizeof(unsigned char) << " 个字节!" << endl; cout << " 最小值:" << (int)(numeric_limits ::min()) << endl; cout << " 最大值:" << (int)(numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之有符号短整型~~" << endl; cout << " 它占: " << sizeof(short) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之无符号短整型~~" << endl; cout << " 它占: " << sizeof(unsigned short) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之有符号整型~~" << endl; cout << " 它占: " << sizeof(int) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之无符号整型~~" << endl; cout << " 它占: " << sizeof(unsigned int) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之有符号长整型~~" << endl; cout << " 它占: " << sizeof(long) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之无符号长整型~~" << endl; cout << " 它占: " << sizeof(unsigned long) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之有符号长长整型~~" << endl; cout << " 它占: " << sizeof(long long) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~整数之无符号长长整型~~" << endl; cout << " 它占: " << sizeof(unsigned long long) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl; cout << " ~~实数之单精度,都是有符号~~" << endl; cout << " 它占: " << sizeof(float) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~实数之双精度,是有符号~~" << endl; cout << " 它占: " << sizeof(double) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; cout << " ~~实数之长双精度,是有符号~~" << endl; cout << " 它占: " << sizeof(long double) << " 个字节!" << endl; cout << " 最小值:" << (numeric_limits ::min()) << endl; cout << " 最大值:" << (numeric_limits ::max()) << endl; cout << endl; //.............................................................. return 0; }
运行截图:



