#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
short x,y;
x=-32654;
y=-1234;
short z=x+y;
cout<<z<<endl;
//cout << (short)x+y<<endl;
return 0 ;
}
至今不明白为什么,,有没有哪位大佬讲一下啊?
华丽的分割线
2018.9.12
解答:现在一看,这么解释?之所以注释掉的那个cout是正确的结果,是因为x强转成short但是x+y是用int相加的。所以答案是int型的所以不会溢出。如果改成cout << (short)(x+y) << endl; 输出的答案也是溢出的。
但是问题来了,为什么会用整型做运算呢?cout自带?