学习目标:
`C++基本数据类型整理成表格,以下是一个表格,展示了不同的基本数据类型以及一般用于和范围大小;
学习内容:
| 数据类型| 描述 |大小(通常情况下)| 用途
| int | 整形 | 至少16位 | 存储整数
| short int | 短整形 | 至少16位 | 存储较小的整数
| long int | 长整形 | 至少32位 | 存储较大的整数
| long long int | 更长整形 | 至少64位 | 存储非常大的整数
| unsigned int | 无符号整形 | 至少16位 | 存储非负整数
| float | 单精度浮点数 | 32位 | 存储小数,精度约6~7位小数
| double | 双精度浮点数 | 64位 | 存储小数,精度约15~16位小数
| long double | 扩展精度浮点数类 | 80位或更多 | 存储小数,提供比double更多的精度
| char | 字符型 | 8位 | 存储单个字符或小整数
| unsigned char | 无符号字符型 | 8位 | 存储较大字符或作为字节使用
| signed char | 有符号字符型 | 8位 | 明确作为带符号的字符或者小整数使用
| bool | 布尔型 | 通常位8位 | 存储真值true或假值false C语言C99以上支持
| wchar_t | 宽字符类型 | 通常位16位或32位 | 存储中文或者unicode
示例代码
#include
#include
using namespace std;
int main()
{
cout << "bool: " << "the size of boll: " << sizeof(bool) << endl;;
cout << "the max value: " << (numeric_limits<bool>::max)() << endl;;
cout << "the min value: " << (numeric_limits<bool>::min)() << endl;
cout << endl;
cout << "int: " << "the size of int: " << sizeof(int) << endl;;
cout << "the max value: " << (numeric_limits<int>::max)() << endl;;
cout << "the min value: " << (numeric_limits<int>::min)() << endl;
cout << endl;
cout << "short int: " << "the size of short int: " << sizeof(short int) << endl;;
cout << "the max value: " << (numeric_limits<short int>::max)() << endl;;
cout << "the min value: " << (numeric_limits<short int>::min)() << endl;
cout << endl;
cout << "long int: " << "the size of long int: " << sizeof(long int) << endl;;
cout << "the max value: " << (numeric_limits<long int>::max)() << endl;;
cout << "the min value: " << (numeric_limits<long int>::min)() << endl;
cout << endl;
cout << "long long int: " << "the size of long int: " << sizeof(long long int) << endl;;
cout << "the max value: " << (numeric_limits<long long int>::max)() << endl;;
cout << "the min value: " << (numeric_limits<long long int>::min)() << endl;
cout << endl;
cout << "float: " << "the size of float: " << sizeof(float) << endl;;
cout << "the max value: " << (numeric_limits<float>::max)() << endl;;
cout << "the min value: " << (numeric_limits<float>::min)() << endl;
cout << endl;
cout << "double: " << "the size of double: " << sizeof(double) << endl;;
cout << "the max value: " << (numeric_limits<double>::max)() << endl;;
cout << "the min value: " << (numeric_limits<double>::min)() << endl;
cout << endl;
cout << "long double: " << "the size of long double: " << sizeof(long double) << endl;;
cout << "the max value: " << (numeric_limits<long double>::max)() << endl;;
cout << "the min value: " << (numeric_limits<long double>::min)() << endl;
cout << endl;
cout << "char: " << "the size of char: " << sizeof(char) << endl;;
cout << "the max value: " << (numeric_limits<char>::max)() << endl;;
cout << "the min value: " << (numeric_limits<char>::min)() << endl;
cout << endl;
cout << "unsigned char: " << "the size of unsigned char: " << sizeof(unsigned char) << endl;;
cout << "the max value: " << (numeric_limits<unsigned char>::max)() << endl;;
cout << "the min value: " << (numeric_limits<unsigned char>::min)() << endl;
cout << endl;
cout << "signed char: " << "the size of unsigned char: " << sizeof(signed char) << endl;;
cout << "the max value: " << (numeric_limits<signed char>::max)() << endl;;
cout << "the min value: " << (numeric_limits<signed char>::min)() << endl;
cout << endl;
cout << "unsigned wchar_t: " << "the size of wchar_t: " << sizeof(wchar_t) << endl;;
cout << "the max value: " << (numeric_limits<wchar_t>::max)() << endl;;
cout << "the min value: " << (numeric_limits<wchar_t>::min)() << endl;
cout << endl;
return 0;
}
运行结果
bool: the size of bool: 1
the max value: 1
the min value: 0
int: the size of int: 4
the max value: 2147483647
the min value: -2147483648
short int: the size of short int: 2
the max value: 32767
the min value: -32768
long int: the size of long int: 4
the max value: 2147483647
the min value: -2147483648
long long int: the size of long int: 8
the max value: 9223372036854775807
the min value: -9223372036854775808
float: the size of float: 4
the max value: 3.40282e+38
the min value: 1.17549e-38
double: the size of double: 8
the max value: 1.79769e+308
the min value: 2.22507e-308
long double: the size of long double: 8
the max value: 1.79769e+308
the min value: 2.22507e-308
char: the size of char: 1
the max value:
the min value:
unsigned char: the size of unsigned char: 1
the max value:
the min value:
signed char: the size of unsigned char: 1
the max value:
the min value:
unsigned wchar_t: the size of wchar_t: 2
the max value: 65535
the min value: 0