问题
Data type int
's minimum value is -2,147,483,648.
So, I typed
int val = -2147483648;
But, it has an error:
unary minus operator applied to unsigned type.result still unsigned
How can I fix it?
回答1:
2147483648
is out of int
range on your platform.
Either use a type with more precision to represent the constant
int val = -2147483648L;
// or
int val = -2147483648LL;
(depending on which type has more precision than int
on your platform).
Or resort to the good old - 1
trick
int val = -2147483647 - 1;
回答2:
-2,147,483,648
is interpreted as negation of 2147483648
. 2147483648
exceed maximum positive integer on your system and is considered as unsigned.
Instead, try
-2147483647 - 1
来源:https://stackoverflow.com/questions/29355956/how-can-i-fix-error-code-c4146-unary-minus-operator-applied-to-unsigned-type-re