02 数据类型
本次记录数据类型,还请各位大佬批评指正!
整型
数据类型 | 表示 | 所占字节 | 范围 |
---|---|---|---|
短整型 | short | 2 | - 2 15 2^{15} 215~ 2 15 2^{15} 215-1 |
整型 | int | 4 | - 2 31 2^{31} 231~ 2 31 2^{31} 231-1 |
长整型 | long | 4 | - 2 31 2^{31} 231~ 2 31 2^{31} 231-1 |
长长整型 | long long | 8 | - 2 63 2^{63} 263~ 2 63 2^{63} 263-1 |
//用sizeof求出整型占用内存大小
int num1 = 10;
cout << "int类型占用内存大小为" << sizeof(num1) << endl;
浮点型
数据类型 | 表示 | 所占字节 | 有效数字 |
---|---|---|---|
单精度浮点数 | float | 4 | 7位 |
双精度浮点数 | double | 8 | 15-16位 |
float num1 = 3.1415926f;
cout << num1 << endl;
cout << "float类型占用内存大小为" << sizeof(num1) << endl;
double num2 = 3.1415926;
cout << num2 << endl;
cout << "double类型占用内存大小为" << sizeof(num2) << endl;
//科学计数法
double num3 = 3e-2; //e后边的数字表示10的多少次方
cout << num3 << endl;
double num4 = 3e2;
cout << num3 << endl;
字符型
1、单引号
2、单引号内只有一个字符
//用sizeof求出字符型占用内存大小
char ch = 'a';
cout << int(ch) << endl; //ASCII编码值
cout << "char类型占用内存大小为" << sizeof(char) << endl;
转义字符
cout << "hello world\n" << endl;
cout << "\\" << endl;
cout << "hello world\tI Love Lu Yao!" << endl;
bool类型
bool ch = true;
cout << ch << endl;
ch = false;
cout << ch << endl;
数据的输入
int a ;
cout << "请输入整型变量" << endl;
cin >> a;
cout << a << endl;
double b;
cout << "请输入浮点型变量" << endl;
cin >> b;
cout << b << endl;
char ab;
cout << "请输入字符型变量" << endl;
cin >> ab;
cout << ab << endl;