2 数据类型
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
2.1 整型
作用:整型变量表示的是整数类型的数据
C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同:
数据类型的占用空间越大,取值范围越广
2.2 sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:
sizeof( 数据类型 )
sizeof( 变量 )
示例:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main() {
//整型: short ( 2 bite ); int ( 4 bite ); long ( 4 bite ); long long ( 8 bite )
short num1 = 10;
cout << "short占用内存空间为:" << sizeof(short) << endl;
cout << "short占用内存空间为:" << sizeof(num1) << endl;
system("pause");
return 0;
}
整型结论:short < int <= long <= long long
2.3 实型(浮点型)
作用:用于表示小数
浮点型变量分为两种:
- 单精度float
- 双精度double
两者的区别在于表示的有效数字范围不同。
数据类型 | 占用空间 | 有效数字范围 |
---|---|---|
float | 4字节 | 7位有效数字 |
double | 8字节 | 15~16位有效数字 |
示例:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main() {
//默认情况下 输出一个小数,会显示6位有效数字
float f1 = 3.1415926f;
double d1 = 3.1415926;
cout << "f1 = " << f1 << endl;
cout << "d1 = " << d1 << endl;
//统计 float 和 double 占用内存空间
cout << "float占用内存空间为:" << sizeof(float) << endl; // 4字节
cout << "double占用内存空间为:" << sizeof(double) << endl; // 8字节
//科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
cout << "f2 = " << f2 << endl; // f2 = 300
float f3 = 3e-2; // 3 * 0.1 ^ 2
cout << "f3 = " << f3 << endl; // f2 = 0.03
system("pause");
return 0;
}
2.4 字符型
作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有一个字符,不可以是字符串
- C和C+中字符型变量只占用1个字节。
- 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元
示例:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main() {
//1、字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//2、字符型变量所点内在大小
cout << "char字符型变量所占内存:" << sizeof(char) << endl;
//3、字符型变量常见错误
//char ch2 = "b": //创建字符型变量时候,要用单引号
//char ch2 = 'abcdef' : //创建字符型变量时侯,单引号内只能有一个字符
//4、字符型变量对应ASCII编码
// a --- 97
// A --- 65
cout << (int)ch << endl;
system("pause");
return 0;
}
a
char字符型变量所占内存:1
97
请按任意键继续. . .
ASCII 码表格
ASCII码 - 基本ASCII码和扩展ASCII码,最全的ASCII码对照表 (asciim.cn)
ASCI码大致由以下两部分组成:
- ASCII 非打印控制字符:ASCII 表上的数字 0-31 分配给了控制字符,用于控制像打印机等一些外围设备。
- ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印文档时就会出现。
2.5 转义字符
作用:用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有:\n \\ \t
转义字符 | 意义 | ASCII码值(十进制) |
---|---|---|
\a | 响铃(BEL) | 007 |
\b | 退格(BS) ,将当前位置移到前一列 | 008 |
\f | 换页(FF),将当前位置移到下页开头 | 012 |
\n | 换行(LF) ,将当前位置移到下一行开头 | 010 |
\r | 回车(CR) ,将当前位置移到本行开头 | 013 |
\t | 水平制表(HT) | 009 |
\v | 垂直制表(VT) | 011 |
\' | 单引号 | 039 |
\" | 双引号 | 034 |
\ | 反斜杠 | 092 |
\ddd | ddd 表示三位八进制数,\ddd 表示 ASCII 编码值为 ddd 的字符 | |
\xdd | dd 表示两位十六进制数,\xdd 表示 ASCII 编码值为 dd 的字符 |
示例:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main() {
//换行符 \n
cout << "hello world\n";
//反斜杠 \\
cout << "\\" << endl;
//水平制表符 \t
cout << "aaaa\thelloworld" << endl;
cout << "aa\thelloworld" << endl;
cout << "aaaaaa\thelloworld" << endl;
system("pause");
return 0;
}
hello world
\
aaaa helloworld
aa helloworld
aaaaaa helloworld
请按任意键继续. . .
2.6 字符串型
作用:用于表示一串字符
两种风格
C风格字符串:
char 变量名[] = "字符串值"
示例:
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; int main() { //注意事项 //1、char 字符串名 [] //2、等号后面 要用双引号 包含起来字符串 char str1[] = "hello world"; cout << str1 << endl; system("pause"); return 0; }
注意:C风格的字符串要用双引号括起来
C++风格字符串:
string 变量名 = "字符串值"
示例:
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; #include <string> //用C++风格字符串时候,要包含这个头文件 int main() { // 注意事项 // 要包含一个头文件 #include <string> string str2 = "hello world"; cout << str2 << endl; system("pause"); return 0; }
注意:C++风格字符串,需要加入头文件#include<string>
2.7 布尔类型bool
作用:布尔数据类型代表真或假的值
bool 类型只有两个值:
- true --- 真(本质是1)
- false --- 假(本质是0)
bool 类型占1个字节大小
示例:
#include <iostream>
using namespace std;
int main() {
bool flag = true;
cout << flag << endl;
flag = false;;
cout << flag << endl;
cout << "size of bool: " << sizeof(bool) << endl;
system("pause");
return 0;
}
1
0
size of bool: 1
请按任意键继续. . .
2.8 数据的输入
作用:用于从键盘获取数据
关键字:cin
语法:cin >> 变量
示例:
#include <iostream>
#include <string> //string头文件
using namespace std;
int main() {
//1、整型
int a = 0;
cout << "请给整型变量a赋值:" << endl;
cin >> a;
cout << "a = " << a << endl;
//2、浮点型
float f = 3.14f;
cout << "请给浮点型变量f赋值:" << endl;
cin >> f;
cout << "f = " << f << endl;
//3、字符型
char ch = 'a';
cout << "请给字符型变量ch赋值:" << endl;
cin >> ch;
cout << "ch = " << ch << endl;
//4、字符串型
string str = "hello";
cout << "请给字符串型 str 赋值:" << endl;
cin >> str;
cout << "str = " << str << endl;
//5、布尔类型
bool flag = false;
cout << "请给布尔类型 flag 赋值:" << endl;
cin >> flag; //bool类型 只要是非0的值都代表真
cout << "flag = " << flag << endl;
system("pause");
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。