1

Why does Visual Studio treats the constant -2147483648 (0x80000000) as unsigned?

From what I know this value is still within min limit for long.

Example, if you compile the following:

long a = -2147483648

The compiler will issue the following warning:

warning C4146: unary minus operator applied to unsigned type, result still unsigned type 
phuclv
  • 37,963
  • 15
  • 156
  • 475
rommel1193
  • 11
  • 1
  • 1
    https://msdn.microsoft.com/en-us/library/4kh09110.aspx – Luca Jul 24 '15 at 05:56
  • The relevant portion: "You can avoid C4146 by using INT_MIN from limits.h, which has the type signed int." – Amadan Jul 24 '15 at 05:58
  • Possible duplicate of one of the following questions: [Why it is different between -2147483648 and (int)-2147483648](http://stackoverflow.com/q/12620753/995714), [large negative integer literals](http://stackoverflow.com/q/8511598/995714), [Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)](http://stackoverflow.com/q/11536389/995714), [(-2147483648> 0) returns true in C++?](http://stackoverflow.com/q/14695118/995714) – phuclv Jul 24 '15 at 06:32
  • thank you, guys. I should have check msdn before posting. – rommel1193 Jul 25 '15 at 10:04

2 Answers2

4

It's because the decimal constant here is not -2147483648, it's 2147483648 with the unary - operator applied to it. In C, numeric constants don't include signs.

2147483648 is out of range for long on your implementation, so under the C89 rules this has type unsigned long. Under the rules introduced in C99, it would instead have type long long.

To change this code to produce the value -2147483648 with type long, you can use:

long l = -2147483647 - 1;
caf
  • 233,326
  • 40
  • 323
  • 462
1

Thats because maximum signed long could be 7FFFFFFF = 0111 1111 1111 1111 1111 1111 1111 1111.

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33