Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix error code C4146 "unary minus operator applied to unsigned type.result still unsigned"?

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?

like image 968
양기창 Avatar asked Mar 30 '15 20:03

양기창


2 Answers

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;
like image 193
AnT Avatar answered Oct 24 '22 08:10

AnT


-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
like image 38
AlexD Avatar answered Oct 24 '22 09:10

AlexD