C++ 数据处理(二)---进制、数据类型

本文介绍了C++中进行进制转换的方法,包括输出十进制、十六进制和八进制值的示例。同时,提到了C++的自动数据类型`auto`以及使用`decltype`关键字进行类型推断的功能,还有类型强转的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



//符号常量

#include<climits>

short s = SHRT_MAX;
    int a = INT_MAX;
    char c = CHAR_MAX;
    cout << sizeof(a) << endl << sizeof(float)  << endl << sizeof(c) <<endl << sizeof(s) << endl;




//C++赋值语法

int bb(100);
    int bbb(0);
    int bbbb = (11);
    
    int cc = {200}; //200
    int ccc = {0};   // 0
    int cccc{11};



//C++进制转换

int d = 200;   //200   10进制
    int dd = 200;  //0xc8  16进制 头两位0x或者0X 其它a~F 0~9
    int ddd = 200; //0310  8进制 第一位0  其它1~7
    
    cout << "输出十进制值"  << endl << d <<endl;  //默认cout的输出是十进制,也可以显式加标识符
    cout << "输出十进制值"  << endl  << dec << d <<endl;

    cout << "输出十六进制值"  << endl  << hex << dd <<endl;
    
    cout << "输出八进制值"  << endl  << oct << ddd <<endl;

输出十进制值

200

输出十进制值

200

输出十六进制值

c8

输出八进制值

310



//C++ 数组语法,省略等号

const int code = 10;
    char cc[] {'a','y'};

    char *q = "sdsdf";
    char qq = 'a';
    char qqq[] = "sdfsdf";
    char qqqq[] = {'q','q'};
    char *qqqqq[] = {"sdf"};


//自动数据类型 auto (比较少普及)

auto a_c = 'a'; //char
    auto a_f = 1.0; //float
    auto a_i = 1;  //int
    
    size_t s_a_c = sizeof(a_c);  //1字节
    size_t s_a_f = sizeof(a_f);  //8字节
    size_t s_a_i = sizeof(a_i);  //4字节



//关键字类型 decltype

float x = 10.0f;
    float &d = x;
    decltype(x) y;         // y type float
    decltype(buy(20)) yy;  // yy type buy return int
    decltype(d) yyy = x;   // yyy type float &
    decltype(d+d) y4;      // y4 type float



//强转

char t = 88;
    char t2 = 'X';
    int a = (int)t;
    a = (int)t2;
    cout << t << endl << t2 << endl;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值