Article directory
1. Notes
Function : Add some instructions and explanations to the code, so that you or other programmers can read the code.
There are two kinds of annotations in C++:
-
Single-line comment : // description information
- Usually placed above a line of code, or at the end of a statement, to explain the line of code.
-
Multi-line comment (delimiter pair comment) : /* description information */
- Usually placed above a line of code, it describes the code as a whole.
Tip: When the compiler compiles the code, it will ignore the content of the comment
Example :
#include <iostream>
using namespace std;
/*
* 简单的主函数:
* 读取两个数,求它们的和
*/
int main( )
{
// 提示用户输入两个数
cout << "Please Enter two numbers:" << endl;
int v1 = 0, v2 = 0; // 保存我们读入的输入数据的变量
cin >> v1 >> v2; // 读取输入数据
cout << "The sum of " << v1 << "and" << v2
<< " is " << v1 + v2 << endl;
return 0;
}
2. Variables
Function : Name a specified memory space to facilitate the operation of this memory
Syntax : data type variable name = initial value;
Example :int num = 100;
3. Constants
Function : used to record unchangeable data in the program
There are two ways to define constants in C++:
- #define macro constants:
#define 常量名 常量值
- Usually defined above the file, representing a constant
- Variables modified by const:
const 数据类型 常量名 = 常量值
- Usually, the keyword const is added before the variable definition to modify the variable as a constant and cannot be modified
Example :
#include <iostream>
using namespace std;
#define WEEK_DAY 7
int main()
{
cout << "一周共有 " << WEEK_DAY << " 天"<< endl;
const int month = 12;
cout << "一年共有 " << month << " 个月" << endl;
return 0;
}
4. Keywords
Role : keywords are pre-reserved words (identifiers) in C++
Tip: When defining variables or constants, do not use keywords
The C++ keywords are as follows:
5. Identifier
Role : The name used to identify variables, functions, classes, modules, or any other user-defined items.
Naming rules :
- Identifiers cannot be keywords;
- Identifiers can only consist of letters, numbers, and underscores
- The first character must be a letter or underscore
- Identifier Chinese letters are case sensitive
Suggestion: When naming an identifier, strive to achieve the effect of knowing the name and knowing it, so that it is convenient for yourself and others to read