Table of contents
2. sizeof and typeid keywords:
1. Constants:
1. Macro constants:
#define macro constants: #define 常量名 常量值
-
Usually defined above the file to represent a constant.
-
Macro constants can be placed in array definitions.
-
Macro constants cannot be modified either.
-
Define without a semicolon at the end
2. const constant:
const modified variablesconst 数据类型 常量名 = 常量值
-
Usually, the keyword const is added before the variable definition to modify the variable as a constant, which cannot be modified and must be assigned an initial value when defining
//1、宏常量
#define day 7
#define n 10
int main() {
//1、宏常量
cout << "一周里总共有 " << day << " 天" << endl;
//day = 8; //报错,宏常量不可以修改
//定义数组
int arr[n];
//2、const修饰变量
const int month = 12;
cout << "一年里总共有 " << month << " 个月份" << endl;
//month = 24; //报错,常量是不可以修改的
system("pause");
return 0;
}
2. Identifier naming rules:
-
Identifier cannot be a keyword
-
Identifiers can only consist of letters, numbers, and underscores
-
The first character must be a letter or underscore
-
Letters in identifiers are case sensitive
3. Data type:
1. Integer:
Different types of integers occupy different spaces:
type of data | take up space | Ranges |
---|---|---|
short (short integer) | 2 bytes | (-2^15 ~ 2^15-1) |
int (integer) | 4 bytes | (-2^31 ~ 2^31-1) |
long (long shaping) | Windows is 4 bytes, Linux is 4 bytes (32-bit), 8 bytes (64-bit) | (-2^31 ~ 2^31-1) |
long long (long long shaping) | 8 bytes | (-2^63 ~ 2^63-1) |
2. sizeof and typeid keywords:
The role of sizeof: use the sizeof keyword to == the memory size occupied by the statistical data type ==
grammar: sizeof( 数据类型 / 变量)
int main() {
cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
return 0;
}
Typeid function: determine what type the data belongs to.
Syntax: typeid (judgment data)
int main()
{
//两个小数相除
double d1 = 0.5;
double d2 = 0.25;
if (typeid(d1/d2) == typeid(double))
{
cout << "是浮点类型" << endl;
}
else if (typeid(d1 / d2) == typeid(int)) {
cout << "是整数类型" << endl;
}
cout << d1 / d2 << endl; //double浮点类型
return 0;
}
3. Floating point and scientific notation:
There are two types of floating-point variables used to represent decimals. The difference between the two is that the range of numbers represented is different.
-
single precision float
-
double precision double
type of data | take up space | valid number range |
---|---|---|
float | 4 bytes | 7 significant figures (including integer part) |
double | 8 bytes | 15 to 16 significant figures (including the integer part) |
int main() {
float f1 = 3.14f;
double d1 = 3.14;
//科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
float f3 = 3e-2; // 3 * 0.1 ^ 2
system("pause");
return 0;
}
4. Character type:
Role: character variables are used to display a single character
grammar:char ch = 'a';
Note 1: When displaying character variables, enclose the characters in single quotes instead of double quotes
Note 2: There can only be one character in single quotes, not a string
-
Character variables in C and C++ only occupy 1 byte.
-
A character variable does not store the character itself in the memory, but puts the corresponding ASCII code into the storage unit
-
The ASCII of A is 65, a is 97, and 0 is 48.
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
//ch = "abcde"; //错误,不可以用双引号
//ch = 'abcde'; //错误,单引号内只能引用一个字符
// 将字符转化为对应的ascll值
cout << (int)ch << endl; //查看字符a对应的ASCII码
ch = 97; //可以直接用ASCII给字符型变量赋值
cout << ch << endl;
// 将int转化为对应的ASCII字符
cout << (char)65 << endl;
// 将数字转化为对应的ASCII值
cout << 65 + '0' << endl;
system("pause");
return 0;
}
5. Escape character:
Function: used to represent some ASCII characters that cannot be displayed. Use \ to escape:
- Escape characters can be enclosed in " " double quotes or ' ' single quotes
int main() {
// \
cout << "\\" << endl;
// 在一个tab的距离之后输出Hello
cout << "\tHello" << endl;
//换行相当于打了一个endl
cout << "\n" << endl;
system("pause");
return 0;
}
6. String type:
1. C-style string :char 变量名[] = "字符串值"
2. C++ style string : #include<string> header file string 变量名 = "字符串值" 使用前必须导入
int main() {
//C语言的字符串类型
char str1[] = "hello world";
cout << str1 << endl;
//C++上的字符串类型
string str = "hello world";
cout << str << endl;
system("pause");
return 0;
}
7. Boolean type bool
Role: Boolean data type represents true or false value, bool type has only two values: 1 byte
-
true --- true (essentially 1)
-
false --- false (essentially 0)
int main() {
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
cout << "size of bool = " << sizeof(bool) << endl; //1
system("pause");
return 0;
}
8. Data input:
Keyword: cin
grammar: cin >> 变量
Except for the string type, the input of other types must be given an initial value to use cin, otherwise an error will be reported.
String input:
- Can not enter a space, enter a space to indicate the end of the input
bool type input:
- When entering a number, only when 0 is entered is 0, and the rest of the numbers are all 1
- Enter a string and eventually return 0
int main(){
//字符串型输入
string str;
cout << "请输入字符串型变量:" << endl;
cin >> str;
cout << str << endl;
//布尔类型输入
bool flag = true;
cout << "请输入布尔型变量:" << endl;
cin >> flag;
cout << flag << endl;
system("pause");
return EXIT_SUCCESS;
}
Fourth, the operator:
We mainly introduce the following operators:
operator type | effect |
---|---|
arithmetic operator | For processing four arithmetic operations |
assignment operator | Used to assign the value of an expression to a variable |
comparison operator | Used to compare expressions and return a true or false value |
Logical Operators | Used to return true or false based on the value of an expression |
1. Arithmetic operators:
Function : used to process four operations
operator | the term | example | result |
---|---|---|---|
+ | plus sign | +3 | 3 |
- | negative | -3 | -3 |
+ | add | 10 + 5 | 15 |
- | reduce | 10 - 5 | 5 |
* | take | 10 * 5 | 50 |
/ | remove | 10 / 5 | 2 |
% | modulo (remainder) | 10 % 3 | 1 |
++ | Pre-increment | a=2; b=++a; | a=3; b=3; |
++ | post-increment | a=2; b=a++; | a=3; b=2; |
-- | pre-decrement | a=2; b=--a; | a=1; b=1; |
-- | post-decrement | a=2; b=a--; | a=1; b=2; |
1. \ is the escape symbol, / is the division
2. The value of a will change whether it is a pre-computation or a post-computation.
Addition, subtraction, multiplication and division:
int main()
{
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl; //两个整数相除结果依然是整数
int a2 = 10;
int b2 = 20;
cout << a2 / b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 / b3 << endl; //报错,除数不可以为0
//两个小数可以相除
double d1 = 0.5;
double d2 = 0.25;
if (typeid(d1/d2) == typeid(double))
{
cout << "是浮点类型" << endl;
}
else if (typeid(d1 / d2) == typeid(int)) {
cout << "是整数类型" << endl;
}
cout << d1 / d2 << endl; //还是浮点类型
system("pause");
return 0;
}
Model:
1. Only integers can take modulo operations
2. The modulo operation also does not support divisor by 0
//取模
int main() {
int a1 = 10;
int b1 = 3;
cout << 10 % 3 << endl;
int a2 = 10;
int b2 = 20;
cout << a2 % b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 % b3 << endl; //取模运算时,除数也不能为0
//两个小数不可以取模
double d1 = 3.14;
double d2 = 1.1;
//cout << d1 % d2 << endl;
system("pause");
return 0;
}
2. Assignment operator:
Role: used to assign the value of an expression to a variable
operator | the term | example | result |
---|---|---|---|
= | assignment | a=2; b=3; | a=2; b=3; |
+= | add equal to | a=0; a+=2; | a=2; |
-= | minus equal to | a=5; a-=3; | a=2; |
*= | multiply equal to | a=2; a*=2; | a=4; |
/= | divide equal to | a=4; a/=2; | a=2; |
%= | Modulus is equal to | a=3; a%2; | a=1; |
3. Comparison operators
Function: Used for comparison of expressions, and returns a true value or a false value.
The return value is true or false, i.e. 0 or 1.
operator | the term | example | result |
---|---|---|---|
== | equal to | 4 == 3 | 0 |
!= | not equal to | 4 != 3 | 1 |
< | less than | 4 < 3 | 0 |
> | more than the | 4 > 3 | 1 |
<= | less than or equal to | 4 <= 3 | 0 |
>= | greater or equal to | 4 >= 1 | 1 |
4 logical operators
Function: used to return a true value or a false value according to the value of the expression
In C++, all numbers are true except 0 which is false
operator | the term | example | result |
---|---|---|---|
! | No | !a | If a is false, !a is true; if a is true, !a is false. |
&& | and | a && b | The result is true if both a and b are true, otherwise false. |
|| | or | a || b | If either a or b is true, the result is true, and if both are false, the result is false. |
//逻辑运算符 --- 非
int main() {
int a = 10;
cout << !a << endl; // 0
cout << !!a << endl; // 1
system("pause");
return 0;
}