char、short、int、unigned int 之间的类型转换

标准数据类型之间会进行 隐式类型的安全转换

转换规则如下:

char→int→unsigned int →long→unsigned long→float→double

↓            

short→int

1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main(){
5 unsigned int ui = 1000;
6 int i = -2000;
7 cout << "ui+i=" << ui+i << endl;//ui+i=4294966296
8
9 short a = 'a';
10 cout << "a=" << a << endl;//97 将short类型隐式转换成int
11
12 char b = 'b';
13 cout << "b=" << b << endl;//b
14
15 cout << "a+b=" << a+b << endl;//195
16 return 0;
17 }

编译器在第⑦行对 i 进行了隐式类型转换

解决办法:

在进行计算的时候确保操作数的数据类型是一致的

原文地址:https://www.cnblogs.com/DXGG-Bond/p/11892396.html