学习C/C++同步进行 第5课 Char\Short\Int\Long\Float\Double\LongDouble---hex\oct\dec

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

/*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;
}
 

运行结果:

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值