编写代码,运行一个helloword
#include
using namespace std;
int main() {
cout << "Hello world" << endl;
system("pause");
return 0;
}
C++的注释:在代码中加一些说明和解释
俩种格式:
1. **单行注释**:`// 描述信息` - 通常放在一行代码的上方,或者一条语句的末尾,==对该行代码说明==
2. **多行注释**: `/* 描述信息 */` - 通常放在一段代码的上方,==对该段代码做整体说明==
注意:在编译的时候会忽略注释代码
变量的定义:给一段指定的内存空间起名,方便操作这段内存
语法:数据类型 变量名 = 初始值;
注意:C++创建变量的时候必须给变量一个初始值,否则程序会报错
常量:用于记录程序中不可更改的数据
C++定义常量的俩种方式:
1. **\#define** 宏常量: `#define 常量名 常量值` * ==通常在文件上方定义==,表示一个常量
2. **const**修饰的变量 `const 数据类型 常量名 = 常量值` * ==通常在变量定义前加关键字const==,修饰该变量为常量,不可修改
例子:
//1、宏常量
#define cup 7
int main() {
cout << "一周里总共有 " << cup << " 天" << endl;
//cup = 8; //报错,宏常量不可以修改
//2、const修饰变量
const int month = 12;
cout << "一年里总共有 " << month << " 个月份" << endl;
//month = 24; //报错,常量是不可以修改的 system("pause");
return 0;
}
C++的关键字:关键字是C++中预先保留的单词(标识符)
注意定义变量或者常量的时候不要用关键字。
| asm | do | if | return | typedef |
| ---------- | ------------ | ---------------- | ----------- | -------- |
| auto | double | inline | short | typeid |
| bool | dynamic_cast | int | signed | typename |
| break | else | long | sizeof | 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 | wchar_t |
| continue | for | public | throw | while |
| default | friend | register | true | |
| delete | goto | reinterpret_cast | try | |
标识符命名规则:C++规定给标识符(变量、常量)命名时,有一套自己的规则
* 标识符不能是关键字
* 标识符只能由字母、数字、下划线组成
* 第一个字符必须为字母或下划线
* 标识符中字母区分大小写