1、数据类型
1.1 整形
2.2 sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存的大小
语法:sizeof(数据类型 / 变量)
#include<iostream>
using namespace std;
int main() {
//cout << "hello world" << endl;
short num1 = 10;
long long num2 = 10;
//sizeof(数据类型/变量)
cout << "short占用的内存空间为:" << sizeof(num1) << endl;
cout << "long long占用的内存空间为:" << sizeof(long long) << endl;
//整形:short (2个字节) int (4) long (4) long long (8)
system("pause");
return 0;
}
2.3 实型(浮点型)
#include<iostream>
using namespace std;
int main()
{
//单精度 float
//双精度 double
//默认情况下 输出一个小数,会显示出6位有效数字
float f1 = 3.1415926f;
cout << "f1 =" << f1 << endl;
double d1 = 3.1415926;
cout << "d1 =" << d1 << endl;
//统计float和double占用内存空间
cout << "float 占用内存空间:" << sizeof(float) << endl;
cout << "double 占用内存空间:" << sizeof(double) << endl;
//科学计数法
float f2 = 3e2; //3 * 10^2
cout << "f2 = " << f2 << endl;
float f3 = 3e-2; //3 * 10^2
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
运行结果:
2.4 字符型
#include<iostream>
using namespace std;
int main() {
char ch = 'a';
cout << ch << endl;
// 2 字符型变量所占内存大小
cout << "字符所占大小:" << sizeof(char) << endl;
//创建字符型变量要用单引号
//创建字符型变量的时候,单引号内只能有一个字符
// 3 字符型变量对应的ASCII编码
cout << (int)ch << endl;
system("pause");
return 0;
}
运行结果:
ASCII码表格:
2.5 注意字符
#include<iostream>
using namespace std;
int main() {
//转义字符
//换行符 \n
cout << "hello world\n";
//反斜杠
cout << "\\" << endl;
//水平制表符 作用可以整齐的输出数据
cout << "aaaa\tccy" << endl;
cout << "aa\tccy" << endl;
cout << "aaaaaa\tccy" << endl;
system("pause");
return 0;
}
运行结果:
2.6 字符串型