基础入门C++(2)—数据类型

网友投稿 449 2022-05-29

数据类型

1 整型

2 sizeof关键字

零基础入门C++(2)—数据类型

3 实型(浮点型)

4 字符型

5 转义字符

6 字符串型

7 布尔类型 bool

8 数据的输入

1 整型

作用:整型变量表示的是

整数类型

的数据

C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同:

2 sizeof关键字

作用: 利用sizeof关键字可以

统计数据类型所占内存大小

语法: sizeof( 数据类型 / 变量)

示例:

#include 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; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

整型结论:

short < int <= long <= long long

3 实型(浮点型)

作用:用于

表示小数

浮点型变量分为两种:

单精度float

双精度double

两者的区别在于表示的有效数字范围不同。

示例:

#include 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

4 字符型

作用: 字符型变量用于显示单个字符

语法:char ch = 'a';

注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号

注意2:单引号内只能有一个字符,不可以是字符串

C和C++中字符型变量只占用

1个字节

字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元

示例:

#include 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; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

ASCII码表格:

ASCII 码大致由以下两部分组成:

ASCII 非打印控制字符: ASCII 表上的数字 0-31 分配给了控制字符,用于控制像打印机等一些外围设备。

ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印文档时就会出现。

5 转义字符

作用: 用于表示一些

不能显示出来的ASCII字符

现阶段我们常用的转义字符有:\n \\ \t

示例:

#include using namespace std; int main() { cout << "\\" << endl; cout << "\tHello" << endl; cout << "\n" << endl; return 0; }

1

2

3

4

5

6

7

8

6 字符串型

作用:用于表示一串字符

两种风格

C风格字符串: char 变量名[] = "字符串值"

示例:

#include #include using namespace std; int main() { string str = "hello world"; cout << str << endl; return 0; }

1

2

3

4

5

6

7

8

注意:C++风格字符串,需要加入头文件==#include==

7 布尔类型 bool

作用: 布尔数据类型代表真或假的值

bool类型只有两个值:

true — 真(本质是1)

false — 假(本质是0)

bool类型占

1个字节

大小

示例:

#include #include 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

2

3

4

5

6

7

8

9

10

11

12

13

8 数据的输入

作用:用于从键盘获取数据

关键字: cin

语法: cin >> 变量

示例:

#include 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; return 0; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

C++ 数据结构

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Gartner:云战略指南
下一篇:对比6款 “数据分析” 工具,学什么,一目了然!
相关文章