数据类型
C++在创建变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
1 整型
作用:整型变量表示的是整数类型的数据
C++中有以下几种方式表示整型,区别在于所占内存空间不同:
数据类型 | 占用空间 | 取值范围 |
---|---|---|
short(短整型) | 2字节 | ( − 2 15 -2^{15} −215 ~ 2 15 − 1 2^{15}-1 215−1 ) |
int(整型) | 4字节 | ( − 2 31 -2^{31} −231 ~ 2 32 − 1 2^{32}-1 232−1 ) |
long(长整型) | windows为4字节,linux为4字节(32位),8字节(64位) | ( − 2 31 -2^{31} −231 ~ 2 31 − 1 2^{31}-1 231−1 ) |
long long(长长整型) | 8字节 | ( − 2 63 -2^{63} −263 ~ 2 63 − 1 2^{63}-1 263−1 ) |
从上表中可以看到,一个整型数据有很多种表示方式,但是,有人会这么想,我就用一个整型不就好了么,搞这么多干嘛,请看下图:
从上图中可以看到,a和b由于数值大小的不同,所需的空间大小也不一致,而所需的空间大小不一致,也导致了整型的取值范围不同,占用空间越多,取值范围越大。
2 sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
示例代码:
#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;
}
结果:
这样,就求出了整型数据所占用的内存空间大小,跟表中一致。
3 浮点型(实型)
作用:用于表示小数
浮点型变量分为两种:
- 单精度float
- 双精度double
两者的区别在于表示的有效数字范围不同。
数据类型 | 占用空间 | 有效数字范围 |
---|---|---|
float | 4字节 | 7位有效数字 |
double | 8字节 | 15~16位有效数字 |
示例代码:
#include<iostream>
using namespace std;
//浮点型
int main()
{
//1、单精度 float
float f1 = 3.14f;//一般来说,float型后面要加上"f",直接告诉程序它是一个单精度,否则,它会先默认3.14为双精度,然后再转换成float型。
//2、双精度 double
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
return 0;
}
结果:
特别说明一下,float和double默认情况下输出的小数会显示出6位有效数字:
在这里插入代码片//1、单精度 float
float f1 = 3.14f;//一般来说,float型后面要加上"f",直接告诉程序它是一个单精度,否则,它会先默认3.14为双精度,然后再转换成float型。
//2、双精度 double
double d1 = 3.1415926;
cout << f1 << endl;
cout << d1 << endl;
4 字符型
作用:字符型变量用于显示单个字符
语法:char c='a';
注意:
- 在显示字符型变量时,用单引号将字符括起来,不要用双引号
- 单引号内只能由一个字符,不可以是字符串
tips:
- C和C++中字符型变量只占用1个字节
- 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元
示例代码:
int main()
{
char ch = 'a';
cout << "ch=" << ch << endl;
//查看字符型对应的ASCII码
//a-97,A-65
cout << "ch-ascii码:" << int(ch) << endl;
cout << sizeof(char) << endl;
//常见错误
//char cha = "b"; //错误
//char ch2 = 'abcdef'; //错误
return 0;
}
结果:
5 转义字符
作用:用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有\n、\\、\t
部分常见的转义字符:
转义字符 | 含义 | ASCII码值(十进制) |
---|---|---|
\a | 警报 | 007 |
\b | 退格(BS),将当前位置移到前一列 | 008 |
\f | 换页(FF),将当前位置移到下页开头 | 012 |
\n | 换行(LF),将当前位置移到下一行开头 | 010 |
\r | 回车(CR),将当前位置移到本行开头 | 013 |
\t | 水平制表(HT),(跳到下一个TAB位置) | 009 |
\v | 垂直制表(VT) | 011 |
\ | 代表一个反斜线字符"" | 092 |
示例代码:
#include<iostream>
using namespace std;
int main()
{
//换行符 \n
cout << "hello world\n" ;
//反斜杠 \\
cout << "\\" << endl ;
//水平制表符 \t
cout << "hello\tworld";
return 0;
}
6 字符串型
作用:用于表示一串字符
两种风格:
- C风格字符串:
char 变量名[]="字符串值"
【C风格的字符串要用双引号】 - C++风格字符串:
string 变量名=“字符串值”
示例代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//C风格,加[],且双引号
char str1[] = "hello world";
cout << "str1=" << str1 << endl;
//C++风格
string str2 = "hello world";
cout << "str2=" << str2 << endl;
return 0;
}
注意:在使用C++风格的代码时,记得引入头文件#include<string>
,虽然现在有些IDE如Visual Studio、VS Code等已经不需要引入该头文件了,但为了保持代码的可读性、规范性与可移植性,最好加上!
7 布尔型
作用:代表真或假的值
bool类型只有两个值:
- true—真【本质是1】
- false—假【本质是0】
注:bool类型占1个字节大小
代码示例:
int main()
{
bool flag = true;
cout << "flag=" << flag << endl; //1
flag = false;
cout << "flag=" << flag << endl; //0
//计算bool占用的空间内存大小
cout << "size of bool=" << sizeof(bool) << endl;//1
return 0;
}
结果:
8 数据的输入
作用:用于从键盘获取输入数据
关键字:cin
语法:cin>>变量
代码示例:
//创建一个变量
int num1 = 10;
cout << "初始num1=" << num1 << endl;
//更改变量
cin >> num1;
cout << "更改后的num1=" << num1 << endl;
结果:
本节所有代码:
#include<iostream>
#include<string>
using namespace std;
//sizeof
void sizeof_example()
{
cout << "short类型所占内存空间为:" << sizeof(short) << endl;
cout << "int类型所占内存空间为:" << sizeof(int) << endl;
cout << "long类型所占内存空间为:" << sizeof(long) << endl;
cout << "long long类型所占内存空间为:" << sizeof(long long) << endl;
}
//浮点型
void type_float_double()
{ //1、单精度 float
float f1 = 3.14f;//一般来说,float型后面要加上"f",直接告诉程序它是一个单精度,否则,它会先默认3.14为双精度,然后再转换成float型。
//2、双精度 double
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
//默认情况下输出的小数会显示出6位有效数字
//科学计数法
float f2 = 3e2;//3*10^2
float f3 = 3e-2;//2*10^-2
cout << f2 << endl;
cout << f3 << endl;
}
//字符型
void cha_type()
{
char ch = 'a';
cout << "ch=" << ch << endl;
//查看字符型对应的ASCII码
//a-97,A-65
cout << "ch-ascii码:" << int(ch) << endl;
cout << sizeof(char) << endl;
//常见错误
//char cha = "b"; //错误
//char ch2 = 'abcdef'; //错误
}
//转义字符
void trans_cha()
{
//换行符 \n
cout << "hello world\n" ;
//反斜杠 \\
cout << "\\" << endl ;
//水平制表符 \t
cout << "hello\tworld";
}
//字符串型
void cha_length()
{
//C风格,加[],且双引号
char str1[] = "hello world";
cout << "str1=" << str1 << endl;
//C++风格
string str2 = "hello world";
cout << "str2=" << str2 << endl;
}
//bool类型
void bool_type()
{
bool flag = true;
cout << "flag=" << flag << endl; //1
flag = false;
cout << "flag=" << flag << endl; //0
//计算bool占用的空间内存大小
cout << "size of bool=" << sizeof(bool) << endl;//1
}
//输入型
void cin_type()
{
//创建一个变量
int num1 = 10;
cout << "初始num1=" << num1 << endl;
//更改变量
cin >> num1;
cout << "更改后的num1=" << num1 << endl;
}
int main()
{
//sizeof_example();
//type_float_double();
//cha_type();
//trans_cha();
//cha_length();
//bool_type();
cin_type();
return 0;
}