当前位置:网站首页>2. 数据类型
2. 数据类型
2022-06-21 12:06:00 【我想要去航海】
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存。
2.1 整型
作用:整型变量表示的时整型类型的数据。
C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同
| 数据类型 | 占用空间 |
|---|---|
| short(短整型) | 2字节 |
| int(整型) | 4字节 |
| long(长整型) | W为4字节,L为4字节(32),8字节(64) |
| long long(长长整型) | 8字节 |
2.2 sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法:sizeof(数据类型/变量)
示例:
#include<iostream>
using namespace std;
int main()
{
int a=0;
cout<<sizeof(a)<<endl;
cout<<sizeof(short)<<endl;
cout<<sizeof(int)<<endl;
cout<<sizeof(long)<<endl;
cout<<sizeof(long long)<<endl;
system("pause");
return 0;
}
2.3 实型(浮点型)
作用:用于表示小数。
浮点型变量分为两种:
1. 单精度 float
2. 多精度 double
两者的区别在于表示的有效数字范围(小数点前和小数点后)不同。
| 数据类型 | 占用空间 | 有效数字范围 |
|---|---|---|
| float | 4字节 | 7位有效数字 |
| double | 8字节 | 15~16位有效数字 |
示例:
#include<iostream>
using namespace std;
int main()
{
float f2=3e2;
double d2=3e2;
cout<<f2<<endl;
cout<<sizeof(f2)<<endl;
cout<<d2<<endl;
cout<<sizeof(d2)<<endl;
system("pause");
return 0;
}
2.4 字符型
作用:字符型变量用于显示单个字符。
语法:char ch=‘a’;
注意:
1. 在显示字符型变量时,用单引号将字符括起来,不要用双引号
2. 单引号内只能有一个字符,不可以是字符串
3. C和C++中字符串变量只占用1个字节
4. 字符型变量并不是把字符本身放在内存中存储,而是将对应的ASCII编码放入到存储单元中。
示例:
#include<iostream>
using namespace std;
int main()
{
char ch='a';
cout<<ch<<endl;
cout<<sizeof(char)<<endl;
cout<<(int)ch<<endl; // 查看字符a对应的ASCII码
ch=97; // 可以直接用ASCII给字符型变量赋值
cout<<ch<<endl;
system("pause");
return 0;
}
2.5 转义字符
作用:用于表示一些不能显示出来的ASCII码。
2.6 字符串型
作用:用于表示一串字符
两种风格之C风格字符串示例:
#include<iostream>
using namespace std;
int main()
{
// 注意:C风格字符串要用双引号括起来
char str1[]="hello world";
cout<<str1<<endl;
system("pause");
return 0;
}
两种风格之C++风格字符串示例:
#include<iostream>
using namespace std;
#include<string>
int main()
{
// 注意:C++风格字符串中,需要加入头文件#include<string>
string str1="hello world";
cout<<str1<<endl;
system("pause");
return 0;
}
2.7 布尔数据类型
作用:布尔数据类型代表真或假的值
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<<sizeof(bool)<<endl;
system("pause");
return 0;
}
2.8 数据的输入
作用:用于从键盘获取数据。
关键字:cin
语法:cin>>变量
示例:
#include<iostream>
using namespace std;
int main()
{
double d=0;
cout<<"请输入浮点型变量:"<<endl;
cin>>d;
cout<<d<<endl;
system("pause");
return 0;
}
边栏推荐
- Typera free version, without cracking, can be installed and used directly
- MySQL-DML
- 动手学数据分析 数据可视化
- Embedded struct and embedded interface
- 蜜雪冰城(已黑化)
- Huawei cloud releases desktop ide codearts
- Some functions used in tensorflow
- Adapter power supply automatic test equipment | introduction to charger ATE test system nsat-8000
- Why are there only 13 root domain name servers in the world
- [comprehensive pen test] sword finger offer II 114 Alien dictionary
猜你喜欢

动手学数据分析 数据重构

i. MX - rt1052 clock and phase locked loop (PLL) analysis

SDCC编译器 + VSCode开发 8位微控制器

异质化社群量化研究4丨RATE OF CHANGE WITH BANDS

创建型模式 - 单例模式

华为是如何从0到1打造以项目为中心运作的项目管理体系的?

搭建zabbix监控及邮件报警

使用Huggingface在矩池云快速加载预训练模型和数据集

南京大学 静态软件分析(static program analyzes)-- Intermediate Representation 学习笔记

MySQL 5.6.49 enterprise version setting password complexity policy
随机推荐
3D Slicer导入标签与查看标签
养老年金险是理财产品吗?预期收益是多少?
Redis最大内存淘汰策略
Vs code + GDB download and debugging of STM32 development
浅论OCA\UV-OCA LOCA\SLOCA 四种全贴合工艺
Related codes of findpanel
Harmonyos training I
i.MX - RT1052时钟及锁相环(PLL)分析
Tensorflower使用指定的GPU和GPU显存
动手学数据分析 数据可视化
Introduction to CPU, MPU, MCU, SOC and MCM
WPF uses Maui's self drawing logic
方法的繼承和重寫
异质化社群量化研究4丨RATE OF CHANGE WITH BANDS
i.MX - RT1052 SDCard操作(SDIO接口)
Héritage et réécriture des méthodes
i.MX - RT1052 脉宽调制(PWM)
动手学数据分析 数据重构
2022年138套数学分析高等代数考研真题参考解答勘误
Knowledge points: several special wiring methods for PCB