简体   繁体   English

如何修复错误代码C4146“unary减运算符应用于unsigned type.result仍未签名”?

[英]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. 数据类型int的最小值为-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? 我该如何解决?

2147483648 is out of int range on your platform. 2147483648超出了您平台上的int范围。

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). (取决于哪种类型的精度比平台上的int更高)。

Or resort to the good old - 1 trick 或者诉诸好老- 1

int val = -2147483647 - 1;

-2,147,483,648 is interpreted as negation of 2147483648 . -2,147,483,648被解释为2147483648否定。 2147483648 exceed maximum positive integer on your system and is considered as unsigned. 2147483648超过系统上的最大正整数,并被视为无符号。

Instead, try 相反,试试吧

-2147483647 - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C:具有无符号操作数的一元减运算符行为 - C: unary minus operator behavior with unsigned operands MISRA 违规 12.9 一元减号的操作数未签名 - MISRA Violation 12.9 Operand of unary minus is unsigned 一元减号和签名到无符号转换 - Unary minus and signed-to-unsigned conversion 一元减0x80000000(有符号和无符号) - unary minus for 0x80000000 (signed and unsigned) 为什么 (-i) 的类型,其中 i 是 unsigned int,仍然是 unsigned int? - Why is the type of (-i), where i is unsigned int, still unsigned int? "使用 MPI 时,如何修复错误:“_int_malloc: Assertion `(unsigned long) (size) >= (unsigned long) (nb)' failed”?" - When using MPI, how can I fix the error: "_int_malloc: Assertion `(unsigned long) (size) >= (unsigned long) (nb)' failed"? 一元运算符和运算符应用于函数的结果 - Result of unary & operator applied to a function 如何解决类似“已签名和未签名之间的比较”之类的警告? - How can I fix warnings like: “comparison between signed and unsigned”? 一元运算符“ - ”对C / C ++(以及不同的编译器)中的无符号数据类型做了什么? - What does the unary operator “-” do on unsigned data types in C/C++ (and on different compilers)? 一元整数4的一元否定 - Unary negation of unsigned integer 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM