1. Data type
C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise memory cannot be allocated to the variable
1.1 Integer
Role : the integer variable representsinteger typeThe data
There are several ways to represent integer types in C++, the difference is that the memory space occupied is different :
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) |
1.2 sizeof keyword
**Function: **Using the sizeof keyword canMemory size occupied by statistical data types
grammar: sizeof( 数据类型 / 变量)
Example:
#include<iostream>
using namespace std;
int main() {
cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;
return 0;
}
Integer conclusion :short < int <= long <= long long
1.3 Real type (floating point type)
Function : fordisplay decimal
There are two types of floating-point variables:
- single precision float
- double precision double
The difference between the two lies in the range of valid numbers represented.
type of data | take up space | valid number range |
---|---|---|
float | 4 bytes | 7 significant figures |
double | 8 bytes | 15 to 16 significant figures |
Example:
#include<iostream>
using namespace std;
int main() {
float f1 = 3.14f;
double d1 = 3.14;
cout << f1 << endl;
cout << d1<< endl;
cout << "float sizeof = " << sizeof(f1) << endl;
cout << "double sizeof = " << sizeof(d1) << endl;
//科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
cout << "f2 = " << f2 << endl;
float f3 = 3e-2; // 3 * 0.1 ^ 2
cout << "f3 = " << f3 << endl;
return 0;
}
1.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 occupy1 byte。
- A character variable does not store the character itself in the memory, but puts the corresponding ASCII code into the storage unit
Example:
#include<iostream>
using namespace std;
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
//ch = "abcde"; //错误,不可以用双引号
//ch = 'abcde'; //错误,单引号内只能引用一个字符
cout << (int)ch << endl; //查看字符a对应的ASCII码
ch = 97; //可以直接用ASCII给字符型变量赋值
cout << ch << endl;
return 0;
}
ASCII code form:
ASCII value | control character | ASCII value | character | ASCII value | character | ASCII value | character |
---|---|---|---|---|---|---|---|
0 | NUT | 32 | (space) | 64 | @ | 96 | 、 |
1 | SOH | 33 | ! | 65 | A | 97 | a |
2 | STX | 34 | " | 66 | B | 98 | b |
3 | ETC | 35 | # | 67 | C | 99 | c |
4 | EOT | 36 | $ | 68 | D | 100 | d |
5 | ENQ | 37 | % | 69 | E | 101 | e |
6 | ACK | 38 | & | 70 | F | 102 | f |
7 | BEL | 39 | , | 71 | G | 103 | g |
8 | BS | 40 | ( | 72 | H | 104 | h |
9 | HT | 41 | ) | 73 | I | 105 | i |
10 | LF | 42 | * | 74 | J | 106 | j |
11 | VT | 43 | + | 75 | K | 107 | k |
12 | FF | 44 | , | 76 | L | 108 | l |
13 | CR | 45 | - | 77 | M | 109 | m |
14 | SO | 46 | . | 78 | N | 110 | n |
15 | AND | 47 | / | 79 | O | 111 | o |
16 | DLE | 48 | 0 | 80 | P | 112 | p |
17 | DCI | 49 | 1 | 81 | Q | 113 | q |
18 | DC2 | 50 | 2 | 82 | R | 114 | r |
19 | DC3 | 51 | 3 | 83 | S | 115 | s |
20 | DC4 | 52 | 4 | 84 | T | 116 | t |
21 | WANT | 53 | 5 | 85 | U | 117 | u |
22 | SYN | 54 | 6 | 86 | V | 118 | v |
23 | TB | 55 | 7 | 87 | W | 119 | w |
24 | CAN | 56 | 8 | 88 | X | 120 | x |
25 | EM | 57 | 9 | 89 | Y | 121 | y |
26 | SUB | 58 | : | 90 | Z | 122 | z |
27 | ESC | 59 | ; | 91 | [ | 123 | { |
28 | FS | 60 | < | 92 | / | 124 | | |
29 | GS | 61 | = | 93 | ] | 125 | } |
30 | RS | 62 | > | 94 | ^ | 126 | ` |
31 | US | 63 | ? | 95 | _ | 127 | OF THE |
The ASCII code roughly consists of the following two parts :
- ASCII Non-Printing Control Characters: Numbers 0-31 on the ASCII table are assigned to control characters used to control some peripherals like printers.
- ASCII Printing Characters: Numbers 32-126 are assigned to characters that can be found on the keyboard and appear when viewing or printing the document.
1.5 Escape characters
Function: used to express someASCII characters that cannot be displayed
At this stage, the escape characters we commonly use are: \n \\ \t
escape character | meaning | ASCII value (decimal) |
---|---|---|
\a | alarm | 007 |
\b | Backspace (BS), move the current position to the previous column | 008 |
\f | Form feed (FF), move the current position to the beginning of the next page | 012 |
\n | Line feed (LF), move the current position to the beginning of the next line | 010 |
\r | 回车(CR) ,将当前位置移到本行开头 | 013 |
\t | 水平制表(HT) (跳到下一个TAB位置) | 009 |
\v | 垂直制表(VT) | 011 |
\\ | 代表一个反斜线字符"" | 092 |
’ | 代表一个单引号(撇号)字符 | 039 |
" | 代表一个双引号字符 | 034 |
? | 代表一个问号 | 063 |
\0 | 数字0 | 000 |
\ddd | 8进制转义字符,d范围0~7 | 3位8进制 |
\xhh | 16进制转义字符,h范围09,af,A~F | 3位16进制 |
示例:
#include<iostream>
using namespace std;
int main() {
cout << "\\" << endl;
cout << "\tHello" << endl;
cout << "\n" << endl;
return 0;
}
1.6 字符串型
作用:用于表示一串字符
两种风格
-
C风格字符串:
char 变量名[] = "字符串值"
, 示例:#include<iostream> using namespace std; int main() { char str1[] = "hello world"; cout << str1 << endl; return 0; }
注意:C风格的字符串要用双引号括起来
-
C++风格字符串:
string 变量名 = "字符串值"
, 示例:#include<iostream> using namespace std; int main() { string str = "hello world"; cout << str << endl; return 0; }
注意:C++风格字符串,需要加入头文件==#include<string>==
1.7 布尔类型 bool
作用: 布尔数据类型代表真或假的值
bool类型只有两个值:
- true — 真(本质是1)
- false — 假(本质是0)
bool类型占1个字节大小
示例:
#include<iostream>
using namespace std;
int main() {
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
cout << "size of bool = " << sizeof(bool) << endl; //1
return 0;
}
1.8 数据的输入
作用:用于从键盘获取数据
关键字: cin
语法: cin >> 变量
示例:
#include<iostream>
using namespace std;
int main(){
//整型输入
int a = 0;
cout << "请输入整型变量:" << endl;
cin >> a;
cout << a << endl;
//浮点型输入
double d = 0;
cout << "请输入浮点型变量:" << endl;
cin >> d;
cout << d << endl;
//字符型输入
char ch = 0;
cout << "请输入字符型变量:" << endl;
cin >> ch;
cout << ch << endl;
//字符串型输入
string str;
cout << "请输入字符串型变量:" << endl;
cin >> str;
cout << str << endl;
//布尔类型输入
bool flag = true;
cout << "请输入布尔型变量:" << endl;
cin >> flag;
cout << flag << endl;
return EXIT_SUCCESS;
}