- 1、C++初识
- 1.1、用C++书写“hello world”
- 1.1.1 C++编译器
- 1.2、注释
- 1.3、变量
- 1.4、常量
- 1.5、关键字
- 1.6、标识符(常量、变量)命名规则
- 2、数据类型
- 2.1 整型
- 2.2 sizeof关键字
- 2.3 实型(浮点型)
- 2.4 字符型
- 2.5 转义字符
- 2.6 字符串型
- 2.7 布尔串型
- 2.8 数据的输入
# includeusing namespace std; int main() { cout << "hello world" << endl; system("pause"); return 0; }
刚开始学,具体我也不知道什么含义,盲猜 cout << “hello world” << endl 应该是相当于python中的print(“hello world” )
1.1.1 C++编译器我是用的Windows visual studio,登录官网直接下载的,此处不做描述了。
参考哔站视频
// 单行注释1.3、变量
作用:给一段指定的内存空间起名,方便操作此段内存空间;
语法:数据类型 变量名 = 初始值;
#include1.4、常量using namespace std; int main() { // 别忘了在“10”后面添加“;”,否则会报错 int a = 10; cout << "a = " << a << endl; system("pause"); return 0; }
定义:用于记录程序中不可更改的数据
类型:
1、宏常量,定义形式:#define 常量名 常量值
通常在文件上方定义,表示一个常量
2、const修饰的变量,定义形式:const 数据类型 变量名 = 常量值
通常在变量前面添加const,修饰变量为常量,不可修改
#include//1、宏常量 #define week 7 using namespace std; int main() { //宏常量输出 cout << "一周有" << week << "天" << endl; //const修饰变量 const int year = 12; cout << "一年有" << year << "月" << endl; system("pause"); return 0; }
输出:
一周有7天 一年有12月 请按任意键继续. . .1.5、关键字
作用:关键字是c++预先保留的单词(标识符)
所以:当定义变量名或常量名就不要使用这些标识符,否则会产生歧义、报错
关键字表格:
1、标识符不能是关键字
2、由字母、数字、下划线组成
3、第一字符必须为字母或下划线
4、字母区分大小写
2.1 整型 2.2 sizeof关键字c++规定在定义常量或变量时需指定数据的类型,否则无法给常量或变量分配内存
指定数据类型的意义:给变量分配合适的内存空间
利用sizeof关键字可以统计数据类型所占的内存大小
语法:sizeof(数据类型 / 变量)
#includeusing namespace std; int main() { short num1 = 10; cout << "short占用内存空间为" << sizeof(num1) << endl; int num2 = 10; cout << "int占用内存空间为" << sizeof(num2) << endl; long num3 = 10; cout << "long占用内存空间为" << sizeof(num3) << endl; long long num4 = 10; cout << "long long占用内存空间为" << sizeof(num4) << endl; system("pause"); return 0; }
输出:
short占用内存空间为2 int占用内存空间为4 long占用内存空间为4 long long占用内存空间为8 请按任意键继续. . .2.3 实型(浮点型)
浮点型包括两种:
1、单精度float
2、双精度double
有效数字不仅包括小数点(.)前面的数字,也包括后面的数字,如3.14的有效数字为3;
#includeusing namespace std; int main() { float f1 = 3.1415926f;//4个字节 cout << "f1=" << f1 << endl; double d1 = 3.1415926;//8个字节 cout << "d1=" << d1 << endl; //科学计数法 float f2 = 3e2;//4个字节 cout << "f2=" << f2 << endl; double f3 = 3e-2;//8个字节 cout << "f3=" << f3 << endl; system("pause"); return 0; }
输出:
f1=3.14159 d1=3.14159 f2=300 f3=0.03 请按任意键继续. . .2.4 字符型
字符型变量用于显示单个字符
语法:char ch = “a”;
#includeusing namespace std; int main() { //字符型变量创建方式 char ch = 'a'; cout << ch << endl; //字符型变量所占内存空间 cout << "char所占内存空间为" << sizeof(ch) << endl; //字符型变量对应ASCII编码 cout << (int)ch << endl; system("pause"); return 0; }
输出:
a char所占内存空间为1 97 请按任意键继续. . .2.5 转义字符
作用:用于表示一些不能显示出来的ASCII字符
现阶段常用的转义字符有:n, , t
#includeusing namespace std; int main() { // 换行符"n" cout << "hello worldn"; // \:输出“” cout << "\" << endl; // 制表符t cout << "hellotworld" << endl; return 0; }
输出:
hello world hello world2.6 字符串型
作用:用于表示一串字符
两种表示形式:
c:char 变量名[] = “字符串值”
c++:string 变量名 = “字符串值”
#includeusing namespace std; #include //用c++形式的字符串,要包含这个头部文件 int main() { // c形式的字符串 char str[] = "hello world"; cout << str << endl; // c++形式的字符串,要包含头部文件 string str1 = "hello kitty"; cout << str1 << endl; return 0; }
输出:
hello world hello kitty2.7 布尔串型
代表真或假的值
true:代表真(本质是1)
false:代表假(本质是0)布尔类型占据1个字节大小
#includeusing namespace std; #include int main() { // 创建布尔数据类型 bool flag = true; cout << flag << endl; bool flag1 = false; cout << flag1 << endl; // 布尔类型占据内存大小 cout << sizeof(flag) << endl; cout << sizeof(flag1) << endl; return 0; }
输出:
1 0 1 12.8 数据的输入
作用:用于从键盘获取数据
关键字:cin
语法:cin << 变量
#includeusing namespace std; #include int main() { // 整型 int a = 0; cout << "请输入整型a的值:" << endl; cin >> a; cout << "a的值为:" << a << endl; return 0; }
输出:
请输入整型a的值: 100 a的值为:100
参考哔站视频



