0

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;
}

Edited by sunshine102030

2
Contributors
1
Reply
20
Views
1 Year
Discussion Span
Last Post by deceptikon
1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.