学习资料及工具:bilibili视频网站黑马程序员匠心之作,BV1et411b73Z,共大概300+集https://www.bilibili.com/video/BV1et411b73Z?p=2
工具:Visual 2015
虽然不是0基础,但是之前感觉之前学过的和没学过一样,所以用0基础视频进行学习,此系列帖子作为笔记使用(主要是字丑)。
目录
正文:
二.数据类型
要求:C++中创建变量或者常量时必须指定数据类型//(创建变量或者常量就是起名,指定类型就是确定名字的姓氏)
意义:给变量分配内存空间
2.1 整形——整数
作用:表示整形类型的数据(四种方式,区别在于所占内存不同)
short取值范围为-32768~32767,输出32800为例
#include <iostream>
using namespace std;
int main()
{
//短整形
short num1 = 32800;
//整形
int num2 = 32800;
//长整型
long num3 = 32800;
//长长整形
long long num4 = 32800;
cout << "输出第1个数为" << num1 << endl;
cout << "输出第2个数为" << num2<< endl;
cout << "输出第3个数为" << num3 << endl;
cout << "输出第4个数为" << num4 << endl;
system("pause");
return 0;
}
结果如下:可以看到溢出short的范围的时候他会自动进入到下一轮。
2.2 sizeof 关键字(这是个关键字)
作用:统计数据类型所占的内存大小//(有了sizeof就可以不用去记住每个数据类型所占的空间)
语法:sizeof(数据类型/语法)
#include <iostream>
using namespace std;
int main()
{
//short(2)、int(4)、long(4)、long long(8)
short num1 = 10;
int num2 = 10;
cout << "short所占的内存空间为:" << sizeof(num1) << endl;//sizeof(变量/数据类型)
cout << "int所占的内存空间为:" << sizeof(int) << endl;//sizeof(变量/数据类型)
system("pause");
return 0;
}
输出结果:
2.3 实型(浮点型)——小数
作用:用于表示小数(两种)
float:(单精度) double(双精度)
区别:有效数字范围不同(用double的多一些,但有时候用float可以减少内存)
#include <iostream>
using namespace std;
int main()
{
//单精度
float num1 = 3.14;
//双精度
double num2 = 3.14;
cout << "输出第1个数为" << num1 << endl;
cout << "输出第2个数为" << num2<< endl;
system("pause");
return 0;
}
输出基本没有区别
注:无论是哪种输出,黑窗口里最多显示7位,想要更多位数up没有教,只是说后面会学。
2.4 字符型
作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注:
- 字符要用单引号引用。
- 一个单引号内只能有一个字符,不能是字符串。
- 字符只占用一个字节。
- 字符型变量的存储不是存储字符本身,而是其对应的ASCll编码。//(套娃保存,编码不需要记忆)
#include <iostream>
using namespace std;
int main()
{
//字符型
char A = 'a';
cout << "输出第1个数为" << A << endl;
cout << "输出第1个数的内存为" <<sizeof(A)<< endl;
cout << "输出对应的ASCII编码" << int(A) << endl;//用int检查a的编码
system("pause");
return 0;
}
输出结果:(a--97,A--65)
2.5 转义字符
作用:用于表示不能显示出来的ASCII。
常用的转义字符:\n \t \\(右斜,左斜为说明)
换行\n = endl 反斜杠\\第一个斜杠代表告诉这是要转义,第二个斜杠为输出
\t水平制表:用于对齐
#include <iostream>
using namespace std;
int main()
{
//换行
cout << "hello world\n" ;
//反斜杠(输出为一个\)
cout << "\\" << endl;
//水平制表
cout << "aaa\thello world" << endl;//\t前面共占8个字节
cout << "aaaaa\thello world" << endl;
cout << "aaaaaaaaaaa\thello world" << endl;//\t前面超出8个字节时,会额外再加8个字节
system("pause");
return 0;
}
2.6 字符串类型
作用:用于表示一串字符串(两种风格)
C语言风格:char 变量名[] = “字符串”
C++风格:string 变量名 = “字符串”(有些需要引用<string>)
#include <iostream>
using namespace std;
#include<string>//C++风格必须引用string(但是后续版本似乎把string并入了iostream中)
int main()
{
//C语言风格
char str[] = "hello world";//用双引号
cout << str << endl;
//C++风格
string str1 = "hello world";
cout << str1 << endl;
system("pause");
return 0;
}
结果如图:
2.7 布尔类型
作用:代表数据的真或假(布尔类型只占用一个字节)
true——真(1)
false——假(0)
#include <iostream>
using namespace std;
#include<string>
int main()
{
bool a = true;//判断ture的值
cout << "输出为 :" << a << endl;
a = false;//判断false的值
cout << "输出为 :" << a << endl;
cout << "bool所占的字节为 :" << sizeof(a) << endl;//查看bool所占字节
system("pause");
return 0;
}
结果如图:
2.8 数据的输入
作用:用键盘输入数据(可主观修改数据)
关键词:cin
语法:cin >> 变量
#include <iostream>
using namespace std;
#include<string>//引用C++格式的字符串
int main()
{
//输入整型
int a = 1;
cout << "请给整型变量a赋值 :" << endl;
cin >> a;
cout << "输出a的值为 :" << a << endl;
//输入浮点型
float b = 0.2;
cout << "请给浮点型b赋值 :" << endl;
cin >> b;
cout << "输出b的值为 :" << b << endl;
//输入字符型
char ch = 'c';
cout << "请给字符型变量ch赋值 :" << endl;
cin >> ch;
cout << "输出ch的值为 :" << ch << endl;
//输入字符串型
string sh = "d";
cout << "请给字符串型变量sh赋值 :" << endl;
cin >> sh;
cout << "输出的sh为 :" << sh << endl;
//输入布尔型
bool flag = true;
cout << "请给bool型变量flag赋值 :" << endl;
cin >> flag;//bool 非零的值即为真(绝对值要大于1)
cout << "输出的flag为 :" << flag << endl;
system("pause");
return 0;
}
输入a=100,b=3.14,ch=a;sh=hello;flag=100,结果如下:
数据类型至此结束。