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