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.
精彩评论