C++基础入门
1. 基础中的基础
1.1 常用cpp文件结构
# include<iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
system("pause");
return 0;
}
1.2 注释
两种格式
- 单行注释: // 描述信息
- 多行注释: /* 描述信息 */
1.3 变量
变量作用:就是为内存内置空间起一个名字
语法
// 数据类型 变量名称 = 变量初始值
int a = 10
1.4 常量
常量作用:用于记录程序中不可更改的数据。
C++定义常量的两种方式:
-
#define宏常量 #define 常量名 常量值
通常定义在文件上方, 表示一个常量。
-
conse修饰的变量 const 数据类型 常量名 = 常量值
通常在变量定义前加关键字const, 修饰该变量为常量,不可修改。
1.5 关键字
作用: 关键字是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 |
1.6 标识符命名规则
- 标识符不能是关键字
- 第一个字符必须为字母或下划线
- 标识符只能由字母、数字、下划线组成
- 标识符中字母区分大小写
1.7 linux下文件运行
基础编译命令如下:
g++ filename.cpp -o output
运行:
./output
参考链接:
黑马程序员