《C++ primer》学习系列 - 第一部分:C++基础 - 章节2:变量和基本类型 - 2.1 基本内置类型

学习系列总链接:

《C++ primer》学习系列

https://blog.csdn.net/qq_22122811/article/details/107972250

 

2.1 基本内置类型

2.1.1 算术类型

C++定义了一套包含算术类型和空类型(void)在内的基本数据类型。其中算术类型包含了字符,整数型,布尔型,浮点数。空类型不对应具体的值,仅用于一些特殊的场合,例如最常见的是,当函数不返回任何值时使用空类型作为返回类型。

wchar_t: 用于确保可以存放机器最大扩展字符集中的任意一个字符;

char16_t和char32_t则为Unicode字符集服务(Unicode是用于表示所有自然语言中字符的标准)

long long: 在C++11中新定义的;

 

内置类型的机器实现:

可寻址的最小内存块为"字节(byte)",存储的基本单元为"字(word)",它通常由几个字节组成。大多数机器的字节由8比特组成,字则由32或64比特构成,也就是4或8字节。

 

建议:如何选择类型

1.执行浮点数用double,因为单精度float精度不够,并且两者计算代价相差无几。

2.在算术表达式中不要用char,只有存放字符时才用它。因为char在不同机器上,可能时有符号,也可能是无符号的,运算容易出现问题,如果想用一个不大的整数,可以使用signed char或者unsigned char;

 

2.1.2 类型转换

浮点型转化为整型:近似处理,结果值将保留浮点数中小数点前的部分;

赋给无符号一个超出范围的值:结果是初始值对无符号型表示数值总数取模后的余数。例如,8比特大小的unsigned char可以表示0至255区间的值,如果我们赋予了一个区间以外的值,则实际的结果是该值对256取模后所得余数。因此,把-1赋给8比特大小的unsigned char所得的结果是255.

赋给有符号一个超出范围的值:结果是未定义的。此时,程序可能继续工作,可能崩溃,也可能生成垃圾数据;

注意:切勿混用带符号和无符号数,否则会出现异常结果;如计算a*b, 中int a=-1,unsigned int b=1,结果就得更具int所占位数而定,在我们的环境上是 4294967295;

一些可能存在疑惑的计算:

#include <iostream>

int main()
{
    unsigned u = 10, u2 = 42;
//    std::cout << u2 - u << std::endl;
    std::cout << u - u2 << std::endl;   // prints 4294967264

    int i = 10, i2 = 42;
//    std::cout << i2 - i << std::endl;
    std::cout << i - i2 << std::endl;   // prints -32

    u = 42;
    i = 10;
    std::cout << i - u << std::endl;    // prints 4294967264
//    std::cout << u - i << std::endl;

    u = 10;
    i = -42;
    std::cout << i + i << std::endl;  // prints -84
    std::cout << u + i << std::endl;  // if 32-bit ints, prints 4294967264

//    i = 10;
//    std::cout << "good" << std::endl;
//    while (i >= 0) {
//        std::cout << i << std::endl;
//        --i;
//    }

//    for (int i = 10; i >= 0; --i)
//        std::cout << i << std::endl;

//    for (unsigned u = 0; u <= 10; ++u)
//        std::cout << u << std::endl;  // prints 0 . . . 10

/* NOTE: the condition in the following loop
         will run indefinitely
    // WRONG: u can never be less than 0; the condition will always succeed
    for (unsigned u = 10; u >= 0; --u)
        std::cout << u << std::endl;
*/
//    u = 11; // start the loop one past the first element we want to print
//    while (u > 0) {
//         --u;        // decrement first, so that the last iteration will print 0
//        std::cout << u << std::endl;
//    }

    // be wary of comparing ints and unsigned
    u = 10;
    i = -42;
    if (i < u)               // false: i is converted to unsigned
        std::cout << i << std::endl;
    else
        std::cout << u << std::endl;   // prints 10

    u = 42; u2 = 10;
//    std::cout << u - u2 << std::endl; // ok: result is 32
    std::cout << u2 - u << std::endl;   // ok:  prints 4294967264
}

及结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值