rommel1193
rommel1193

Reputation: 11

Long min limit in Visual Studio

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 

Upvotes: 1

Views: 196

Answers (2)

caf
caf

Reputation: 239071

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;

Upvotes: 4

Ari0nhh
Ari0nhh

Reputation: 5920

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

Upvotes: 1

Related Questions