Re: INT_MIN as decimal
Jordan Abel wrote:[color=blue]
> Say int is a 16-bit twos-complement type.
> Is the expression (-32768) actually negative?[/color]
Yes, but the expression has type long, not int.
[color=blue]
> Or is it the unsigned int literal 32768 with the unary minus operator
> applied to it, resulting in the value of the expression being an
> unsigned int 32768, which can then trap when being assigned to a
> signed int type[/color]
It's a long literal with the unary minus operator applied.
Since -32768L _is_ within the range of your signed int, it won't trap
on
a corresponding C90 or C99 implementation.
Note that only C99 has the potential for implementation defined signals
on
assignment to a signed integer of a value not in range of that integer.
For
C90 the conversion is always implementation defined (i.e. unspecified
but
legal).
Had you written (-0x8000) the expression would yield a positive
unsigned
value and you might have a problem under C99.
[color=blue]
> [It appears to me that the minus sign is not actually
> part of the literal, since it's not part of a number token][/color]
Correct. All integer constants are non-negative. All floating point
constants are non-negative too. Adding an unary - (or unary +)
makes it a constant expression.
--
Peter
Jordan Abel wrote:[color=blue]
> Say int is a 16-bit twos-complement type.
> Is the expression (-32768) actually negative?[/color]
Yes, but the expression has type long, not int.
[color=blue]
> Or is it the unsigned int literal 32768 with the unary minus operator
> applied to it, resulting in the value of the expression being an
> unsigned int 32768, which can then trap when being assigned to a
> signed int type[/color]
It's a long literal with the unary minus operator applied.
Since -32768L _is_ within the range of your signed int, it won't trap
on
a corresponding C90 or C99 implementation.
Note that only C99 has the potential for implementation defined signals
on
assignment to a signed integer of a value not in range of that integer.
For
C90 the conversion is always implementation defined (i.e. unspecified
but
legal).
Had you written (-0x8000) the expression would yield a positive
unsigned
value and you might have a problem under C99.
[color=blue]
> [It appears to me that the minus sign is not actually
> part of the literal, since it's not part of a number token][/color]
Correct. All integer constants are non-negative. All floating point
constants are non-negative too. Adding an unary - (or unary +)
makes it a constant expression.
--
Peter
Comment