I know that the max value for short data type is 32767 and d=2a=2(20000)=40000 that is greater than 32767
therefore the result of d is in minus d= -25536 while a,b and c displayed as they are scince they are< 32767 ,but the question is from where we get this number -25536 ?

#include <iostream>
using namespace std;
int main()
{
short a, b, c, d;
a=20000; 
b=10000;
c=a+b;
d=2*a;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
system("pause");
return 0;
}

I know that the max value for short data type is 32767

Not necessarily. The C++ standard defines a minimum required range, but compilers are free to make it larger.

but the question is from where we get this number -25536 ?

First I'll note that it's not safe to rely on this behavior. The answer is that your implementation treats signed overflow as wrap-around. Values beyond the maximum positive range wrap around into the negative range.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.