sizeof关键字
#include <iostream>
using namespace std;
int main()
{
short num1 = 10;
cout << "short占用的内存大小为" << sizeof(short) << endl;
int num2 = 10;
cout << "int占用的内存大小为" << sizeof(int) << endl;
long num3 = 10;
cout << "long占用的内存大小为" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用的内存大小为" << sizeof(num4) << endl;
system("pause");
return 0;
}
实型(浮点型)
数据类型 | 占用空间 | 有效数字范围 |
---|
float(单精度) | 4字节 | 7位有效数字(包含小数点前的位数) |
double(双精度) | 8字节 | 15~16位有效数字(包含小数点前的位数) |
#include <iostream>
using namespace std;
int main()
{
float f1 = 3.14f;
double d1 = 3.1415926;
cout << "d1=" << d1 << endl;
cout << "float占用的空间为: " << sizeof(float) << endl;
cout << "double占用的空间为: " << sizeof(double) << endl;
float f2 = 3e2;
cout << "f2=" << f2 << endl;
float f3 = 3e-2;
cout << "f3=" << f3 << endl;
system("pause");
return 0;
}