#include<iostream>
#include<cstdlib >
using namespace std; int main() {
char a=101;
int sum=200;
a+=27;sum+=a;
printf("%d", a);
cout<<sum<<endl;
}
分析:char类型的范围是-128---+127,当a+=27,之后a的值超出可表示范围会变为-128.
a为char型,-128~127,a=101,a+=27后溢出a=-128:
a=127时不溢出 01111111(127补码)
a+=1时溢出 10000000(-128补码)
sum += a;
sum为int型,a(char提升为int)
10000000------->11111111 11111111 11111111 10000000(-128补码)
所以,sum=200-128:00000000 00000000 00000000 11001000
+11111111 11111111 11111111 10000000
----------------------------------------------------------------------------------
00000000 00000000 00000000 01001000 (64+8=72)