这两天看书发现了一些小细节,以前没注意到,列举出来:

1.关于bool型

bool b = 12;
int a = b;

现在将a输出,发现它的值是1,相应b也是1.书上解释如下

  “When we assign one of the nonbool arithmetic types to a bool object, the result is false if the value is 0 and true otherwise.”

2.关于unsigned型

用下面的代码测试

#include<iostream>
using namespace std;
int main()
{
    unsigned char c = -1;
    cout<<(int)c<<endl;
    unsigned u1 = 10, u2 = 40;
    cout<<u2-u1<<endl;
    cout<<u1-u2<<endl;
    
    return 0;
}

输出结果:

255
30
4294967266

由于unsigned的限制,负数都转换成了其补码所对应的无符号整数。PS:代码里u1,u2的类型光写了个unsigned,我一直以为需要再加char,int或者float之类的。我测了下大小发现是4个字节,和int一样,所以应该就是int型了。