2 Data types in C++
C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise memory cannot be allocated to the variable
2.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) |
2.2 sizeof keyword
Role: use the sizeof keyword toMemory size occupied by statistical data types
grammar: sizeof( 数据类型 / 变量)
Example:
int main() {
cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;
system("pause");
return 0;
}
operation result:
short 类型所占内存空间为: 2
int 类型所占内存空间为: 4
long 类型所占内存空间为: 4
long long 类型所占内存空间为: 8
请按任意键继续. . .
Integer conclusion :short < int <= long <= long long
It should be noted that the size of the memory space occupied by data in different versions of the operating system environment will vary.
2.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; //如果不加f,系统将默认3.14为double型,并转换为float
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
cout << "float sizeof = " << sizeof(f1) << endl; // 4字节
cout << "double sizeof = " << sizeof(d1) << endl; // 8字节
//科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
cout << "f2 = " << f2 << endl;
float f3 = 3e-2; // 3 * 0.1 ^ 2
cout << "f3 = " << f3 << endl;
//注意,小数不会全部显示出来
float f4 = 3.1415926f;
cout << "f4 = " << f4 << endl;
system("pause");
return 0;
}
operation result:
3.14
3.14
float sizeof = 4
double sizeof = 8
f2 = 300
f3 = 0.03
f4 = 3.14159
请按任意键继续. . .
2.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:
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl; // 1字节
//ch = "abcde"; //错误,不可以用双引号
//ch = 'abcde'; //错误,单引号内只能引用一个字符
cout << (int)ch << endl; //查看字符a对应的ASCII码
ch = 65; //可以直接用ASCII给字符型变量赋值
cout << ch << endl;
system("pause");
return 0;
}
operation result:
a
1
97
A
请按任意键继续. . .
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.
2.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码值(十进制) |
---|---|---|
\a | 警报 | 007 |
\b | 退格(BS) ,将当前位置移到前一列 | 008 |
\f | 换页(FF),将当前位置移到下页开头 | 012 |
\n | 换行(LF) ,将当前位置移到下一行开头 | 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;
// 水平制表符 \t,作用:可以整齐的输出数据
cout << "\tHello" << endl;
cout << "\tWorld" << endl;
cout << "\thaha" << endl;
//换行符 \n
cout << "\n" << endl;
cout << "Hello" << endl;
system("pause");
return 0;
}
运行结果:
\
Hello
World
haha
Hello
请按任意键继续. . .
2.6 字符串型
作用: 用于表示一串字符
两种风格
-
C风格字符串:
char 变量名[] = "字符串值"
示例:
int main() { char str1[] = "hello world"; cout << str1 << endl; system("pause"); return 0; }
注意:C风格的字符串要用双引号括起来
-
C++风格字符串:
string 变量名 = "字符串值"
示例:
int main() { string str = "hello world"; cout << str << endl; system("pause"); return 0; }
注意:C++风格字符串,需要加入头文件 #include<string>
2.7 布尔类型 bool
作用: 布尔数据类型代表真或假的值
bool类型只有两个值:
- true — 真(本质是1)
- false — 假(本质是0)
bool类型占1个字节大小
示例:
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;
}
2.8 数据的输入(cin关键字)
作用:用于从键盘获取数据
关键字: cin
语法: cin >> 变量
示例:
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;
system("pause");
return EXIT_SUCCESS;
}
布尔类型,只要是非0的值,都代表真