开发者

Is -5 an integer literal?

开发者 https://www.devze.com 2023-01-20 10:38 出处:网络
Is -5开发者_JAVA百科 an integer literal? Or is 5 a literal, and -5 is an expression with unary minus taking a literal as an argument? The question arose when I was wondering how to hardcode smallest s

Is -5开发者_JAVA百科 an integer literal? Or is 5 a literal, and -5 is an expression with unary minus taking a literal as an argument? The question arose when I was wondering how to hardcode smallest signed integer values.


It's a unary minus followed by 5 as an integer literal. Yes, that makes it somewhat difficult to represent the smallest possible integer in twos complement.


As Jerry Coffin said, the minus sign is not part of the literal. As for how to solve your ultimate question,

I was wondering how to hardcode smallest signed integer values

That's what INT_MIN (and the like in limits.h or stdint.h or wherever) is for.

If you look at how INT_MIN is defined, it'll probably look something like (-2147483647 - 1) to work around the problem raised by the question.

0

精彩评论

暂无评论...
验证码 换一张
取 消