基础知识
从网上学习的一些知识,自己整理一下,方便自己复习,督促自己每天学习。
基本知识
注释
/*
这是我的第一段代码
*/
#include <iostream>
using namespace std;
int main() {
//这是输出一行 HelloWorld!
cout<<"Hello World!"<<endl;
return 0;
}
变量
作用:给一段指定的内存空间起名,方便操作这段内存
#include <iostream>
using namespace std;
int main() {
int a = 1;
cout<<"a = "<<a<<endl;
return 0;
}
常量
作用:用于记录程序中不可更改的数据
C++中定义常量的两种方式
1.#defin宏常量:通常在文件上方定义,表示一个常量
2.const修饰的变量:通常在变量定义前加关键字const,修饰该变量为常量,不可修改
#include <iostream>
using namespace std;
#define Day 7
int main() {
//此处修改提示错误,Day是常量不可修改
//Day = 14;
cout<<"一周总共有"<<Day<<"天"<<endl;
const int mouth = 12;
//此处修改提示错误,mouth经过const标记是常量,不可修改
//mouth = 24;
cout<<"一年总共有"<<mouth<<"月"<<endl;
return 0;
}
关键字
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 |