Suppose that on a platform, the minimal signed value is −2^15 = −32768 and the maximum value is 2^15 −1 = 32767. The constant 32768 then doesn’t fit into
signed
and is thussigned long
. As a consequence, the expression -32768 has typesigned long
. Thus the minimal value of the typesigned
on such a platform cannot be written as a literal constant.From Modern C by Jens Gustedt.
Its trivial to understand why 32768 is signed long, but why is -32768 also signed long and not signed, given the minimum signed value of -32768?
CodePudding user response:
Because -32768 is the unary operator - applied to 32768.
If you write e.g. ( ( - 32767 ) - 1 ), you get your constant in int type.
CodePudding user response:
If int
is a 16-bit type in your platform then 32768 doesn't fit in it. Therefore 32768
must have the next larger type which is long
. -32768
isn't a negative integer literal but a unary minus applied to the literal 32768
. Since 32768
is long
, so is -32768
This is exactly the same as