C++学习之路抓紧跑路版(二)之C++基本类型
前言
今天我们来学习一下C++的基本类型和每一种类型所占用的大小。
一、C++数据类型的分类
C++的数据类型分为基本数据类型和非基本数据类型。其中基本数据类型包括:整形(int)、字符型(char)、实型:单精度(float)、双精度(double)、布尔型(bool)和无值型(void)。我们来看一下每一种类型占多少个字节。
1、基本数据类型:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"int: "<<sizeof(a)<<endl;
char b;
cout<<"char: "<<sizeof(b)<<endl;
float c;
cout<<"float: "<<sizeof(c)<<endl;
double d;
cout<<"double: "<<sizeof(d)<<endl;
bool e;
cout<<"bool: "<<sizeof(e)<<endl;
}
输出:
int: 4
char: 1
float: 4
double: 8
bool: 1
其中void占用字节数为0。
2、非基本数据类型:
非基本数据类型包括:数组(type[])、指针(type*)、引用(type&)、类(class)、结构体(struct)、联合(union)、枚举(enum)。非基本数据类型之后会每一个专门出一期来依此讲解。
总结
今天的内容就是这些啦~下期把每一个非基本数据类型好好说一下。
本文介绍了C++的基本数据类型,如整型、字符型、浮点型、布尔型和void,并展示了它们在内存中的占用字节数。后续将深入探讨非基本数据类型。
1万+

被折叠的 条评论
为什么被折叠?



