2.数据类型
存在的意义:给变量分配合适的内存空间; 取值范围不一样
- short 2个字节
- int 4个字节
- long 8个字节(64位)
- long long 8个字节
2.1 整型
2.2 sizeof
关键字
**作用:**利用sizeof
关键字统计数据类所占内存的大小
语法:sizeof(数据类型/变量)
示例:
#include <iostream>
using namespace std;
int main(){
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "long long : \t" << "所占字节数:" << sizeof(long long );
cout << "\t最大值:" << (numeric_limits<long long >::max)();
cout << "\t最小值:" << (numeric_limits<long long >::min)() << endl;
return 0;
}
运行结果:
short: 所占字节数:2 最大值:32767 最小值:-32768
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
long long : 所占字节数:8 最大值:9223372036854775807 最小值:-9223372036854775808
结论: short < int <= long <=long long
2.3实型(浮点型)
作用:表示小数
- 单精度 float 占用空间4字节 7位有效数字
- 双精度 double 占用空间8字节 15-16位有效数字
示例:
int main() {
//默认情况下 输出一个小数,会显示出6位有效数字
float f1 = 3.1415926f; // 加f 否则默认是双精度
cout << "f1 " << f1 << endl;
double d1 = 3.1415926;
cout << "d1" << d1 << endl;
//统计 float double 占用的内存空间
cout << "float的内存空间 " << sizeof(float) << endl;
cout << "float的内存空间 " << sizeof(double) << endl;
//科学计数法
float f2 = 3e2; //3*10^2
cout << "f2= " << f2 << endl;
float f3 = 3e-2;
cout << "f3= " << f3 << endl;
return 0;
}
运行结果:
f1 3.14159
d13.14159
float的内存空间 4
float的内存空间 8
f2= 300
f3= 0.03
2.4 字符型
作用:字符型变量用于显示单个字符
语法:char(ch)= 'a';
显示字符变量的时候,用单引号将字符括起来,不要用双引号;
单引号内只能有一个字符,不可以是字符
- c和c++中字符型变量只占用1个字节
- 字符型变量并不是把字符本身放在内存储存,而是将对应ASCII编码储存单元
示例:
#include <iostream>
using namespace std;
int main() {
//字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//字符型变量所占内存大小
cout << "char字符型变量所占的内存:" << sizeof(char)<< endl;
//字符型变量对应的ASCII编码
cout << int(ch) << endl; // a-97 A-65
return 0;
}
运行结果:
a
char字符型变量所占的内存:1
97
2.5 转义字符
作用:用于表示一些不能显示出来的ASCII字符
一般:\n \\ \t
转义字符 | 含义 | ASCII |
---|---|---|
\a | 警报 | 007 |
\b | 退格 | 008 |
\n | 换行符 | 010 |
\t | 水平制表符 | 009 |
\\ | 代表一个 \ | 092 |
\‘ | 一个单引号 | 039 |
\" | 一个双引号字符 | 034 |
? | 一个 问号 | 063 |
示例:
#include <iostream>
using namespace std;
int main() {
cout << "hello world \n";
cout << "\\" << endl;
cout << "aa\thello\n";
return 0;
}
运行结果:
hello world
\
aa hello
2.6字符类型
作用:用于表示一串字符
两种风格:
- c语言风格
char 变量名[] = "字符串值"
注意 1 char 字符串名[] 中括号要加[ ]
注意2 等号后面要用双引号才行
- c++风格
string 变量名 = "字符串值"
示例:
#include <iostream>
using namespace std;
#include<string> //c++风格要使用
int main() {
//c风格 注意 char 字符串名[] 中括号要加
// 等号后面要用双引号才行
char str[] = "hello world ";
cout << str << endl;
//c++风格 包含一个头文件
string str2 = "hello world ";
cout << str2 << endl;
return 0;
}
运行结果:
hello world
hello world
2.7 布尔类型
作用:代表真假
- true 真 (1)
- fasle 假 (0)
bool类型占 1个字节大小
示例:
#include <iostream>
using namespace std;
int main() {
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
return 0;
}
运行结果:
1
0
2.8 数据输入
作用:从键盘获取数据
关键字:cin
语法:cin >> 变量
示例:
#include<string>
#include <iostream>
using namespace std;
int main() {
//1.整型
int a = 0;
cout << "请给整型赋值" << endl;
cin >> a;
cout << "整型变量a = " << a << endl;
//浮点型
float f = 3.14f;
cout << "请给浮点赋值" << endl;
cin >> f;
cout << "浮点型变量f = " << f << endl;
//字符型
char ch = 'a';
//字符串型
string str = "hello";
cout << "请给字符串赋值" << endl;
cin >> str;
cout << "字符串变量str = " << str << endl;
//布尔类型
bool flag = false;
cout << "请给布尔赋值" << endl;
cin >> flag; //布尔类型 只要是非0的值都代表真
cout << "布尔型变量flag = " << flag << endl;
return 0;
}