/*lesson 5*/
#include <iostream>
#include <string>
using namespace std;
#define PI 3.141
const float PAI = 3.141;
int main()
{
string type = "类型占用的字节数 = "; //字符串"类型"声明
cout << "数据类型占用内存:" << endl;
cout << "char" << type << sizeof(char) << endl;
cout << "short" << type << sizeof(short) << endl;
cout << "int" << type << sizeof(int) << endl;
cout << "long" << type << sizeof(long) << endl;
cout << "long long" << type << sizeof(long long) << endl;
cout << "float" << type << sizeof(float) << endl;
cout << "double" << type << sizeof(double) << endl;
cout << "long double" << type << sizeof(long double) << endl;
cout << endl; //换行
int A = 255;
cout << "int类型举例:" << endl;
cout << "默认十进制 变量A = " << A << endl;
cout << "十六进制输出 =" << hex << A << endl;
cout << "八进制输出 =" << oct << A << endl;
cout << "十进制输出 =" << dec << A << endl;
cout << endl; //换行
char B = 'a'; char C = 97;
cout << "char类型举例:" << endl;
cout << "char B= 'a' B = " << B << endl;
cout << "char C= 97 C = " << C << endl;
cout << endl; //换行
float D = 12.3456789f;
cout << "浮点数类型举例:(cout默认输出精度为6位数(整数+小数))" << endl;
cout << "float D= 12.3456789 D = " << D << endl;
cout << "修改输出精度为4位数(整数+小数)" << endl;
cout.precision(4);//修改cout输出精度
cout << "float D= 12.3456789 D = " << D << endl;
cout << "添加定点法输出 精度控制4位数,为小数点后面范围" << endl;
cout.flags(cout.fixed);//定点法输出
cout << "float D= 12.3456789 D = " << D << endl;
cout << "取消定点法输出 精度控制4位数,为整数+小数点后面范围" << endl;
cout.unsetf(cout.fixed);//取消定点法输出
cout << "float D= 12.3456789 D = " << D << endl;
cout << endl; //换行
cout << "char 字面常量表示: 'a','b','X','-'... (字符上加单引号)" << endl;
cout << "long 字面常量表示: 200L (后面加L)" << endl;
cout << "long long 字面常量表示: 100000000000LL (后面加LL)" << endl;
cout << "float 字面常量表示: 3.14f (后面加f)" << endl;
cout << "double 字面常量表示: 3.0,3.14" << endl;
cout << endl; //换行
cout << "符号常量代替字面常量 #define PI 3.141 PI = " << PI << endl;
cout << "符号常量代替字面常量 const float PAI = 3.141; PAI = " << PAI << endl;
cout << endl; //换行
cout << endl; //换行
system("pause"); //程序暂停
return 0;
}
运行结果:

本文通过示例介绍了C/C++中不同数据类型占用的内存大小,包括char、short、int、long、long long、float、double和long double,并演示了如何进行十进制、十六进制和八进制之间的转换。同时,文章还讲解了浮点数的精度控制和符号常量的使用。

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



