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

耗尽温柔 提交于 2019-12-09 15:56:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!