目录
一、注释
注释本身在程序中并不会运行,它的作用仅仅只是我们在我们写的代码中加以注释与标记,让我们的代码看着更加直观,方便后续出错时在源头做出修改。
1.单行注释
单行注释只能在写下的那一行中使用,//加上注释的内容
#include<iostream>
using namespace std;
int main() {
//此行为注释
system("pause");
return 0;
}
2.多行注释
多行注释可以让注释覆盖多行。
#include<iostream>
using namespace std;
int main() {
/*
注释
变量
常量
标识符与关键字
*/
system("pause");
return 0;
}
二、变量
变量的作用:根据数据类型给内存中的某块位置取名,从而方便操控这块内存。
命名方法:数据类型 变量名 = 初始值;
#include<iostream>
using namespace std;
int main() {
//命名方法:数据类型 变量名 = 初始值;
int a = 100;
cout << "a=" << a << endl;
system("pause");
return 0;
}
运行结果可以得到操控a后得到其赋值结果。
三、常量
常量的作用:用于记录程序中不可更改的数据,记录后不可更改,可以防止原有数据被修改。
常量的定义方式有两种:
#define 宏常量(通常在文件上方):#define 常量名 常量值;
const:const 数据类型 常量名 = 常量值;
#include<iostream>
using namespace std;
//#define 常量名 常量值;
#define year_day 365
int main() {
//const 数据类型 常量名 = 常量值;
const int day_hours = 24;
cout << "今年有" << year_day << "天" << endl;
cout << "一天有" << day_hours << "小时" << endl;
system("pause");
return 0;
}
对常量进行修改会出现报错提示:
四、标识符与关键字
1.关键字
定义:c++中预先保留的标识符(单词);
不能用这些关键字给变量,常量等取名。
asm | do | if | return | typedef |
auto | double | inline | short | typename |
bool | dynamic_cast | int | signed | union |
case | enum | mutable | static | unsigned |
catch | explicit | namespace | static_cast | using |
char | export | new | struct | virtual |
class | extern | operator | switch | void |
const | false | private | template | volatile |
const_cast | float | protected | this | while |
continue | for | public | throw | while |
default | friend | register | true | |
delete | goto | reinterpret_cast | try |
2.标识符
定义:我们给变量命名时所取的名字,如上面给变量命名的a为标识符。
命名规则:①标识符不能是关键字。
②只能有字母,数字,下划线组成。
③开头不能为数字。
④标识符字母区分大小写。