1. 整型
// 整型
// 1. 短整型
short num1 = 10;
// 2. 整型
int num2 = 10;
// 3. 长整型
long num3 = 10;
// 4. 长长整型
long long num4 = 10;
2. sizeof
- 可以计算数据类型占用的内存大小
short num1 = 10;
cout << "short占用的内存空间为:" << sizeof(num1) << endl;
int num2 = 10;
cout << "int占用的内存空间为:" << sizeof(num2) << endl;
long num3 = 10;
cout << "long占用的内存空间为:" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用的内存空间为:" << sizeof(num4) << endl;
3. 浮点型
// 1. 单精度
// 2. 双精度
// 默认情况下,输出一个小数,会显示6位有效数字
float fl = 3.1415926f;
cout << "fl:" << fl << endl;
double dl = 3.1415926;
cout << "dl:" << dl << endl;
4. 字符串类型
// 1. C语言风格字符串
char str[] = "Hello World";
cout << str << endl;
// 2. C++风格字符串
string str2 = "Hello World";
cout << str2 << endl;
5. 布尔型
// 创建bool数据类型
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
6. 数据的输入
int a = 0;
cout << "请输入整型变量: " << endl;
cin >> a;
cout << "整型变量a: " << a << endl;
结构体:
- 与数组的最大差别:不同类型数据对象构成的集合
- 当然也可以为相同类型的,但具体意义或解释不同的数据对象集合
- 结构体类型的定义:注意类型定义后面的分号
struct DATE
{
int year;
int month;
int day;
};
- 与数组不同,结构体量可直接赋值,拷贝过程为逐成员一一复制