large negative number multiplied by -1 gives output negative c++
c++, multiplication
Solution
Enable `-Wall` and you'll see the answer.
foo.C:5: warning: this decimal constant is unsigned only in ISO C90
Use `-2147483648LL` in place of your constant.
Problem
Why is the output of this program -2147483648? ``` #include <iostream> using namespace std; int main() { long long a=-2147483648; a=a*-1; cout<<a; return 0; } ``` It should be 2147483648 because it is in range of `long long`. Why the sign is not changing? I have even tried `abs()` function but the result is the same. Also more surprising is that this program outputs 2147483648: ``` #include <iostream> using namespace std; int main() { long long a=-2147483648; a=a*-1; a=a*-1; cout<<a; return 0; } ``` The second time, multiplying by -1 worked. If it matters, I'm using C++ 4.8.1.