sizeof (int) - 1

Pedz Thing pedz at bobkat.UUCP
Thu Oct 2 07:06:29 AEST 1986

In article <102500008 at uiucuxc> grayson at uiucuxc.CSO.UIUC.EDU writes:
>It is interesting that the expression
>	sizeof (int) - 1
>is ambiguous in C, for it can be parsed as
>	sizeof ((int)(- 1))
>or as
>	(sizeof(int)) - 1

At first, I thought this note was really stupid.  I had the idea that
unary minus was down at a different level from the other unary
operators.  Then as I looked more and more into it, I came to these
conclusions.  First, what the compiler does is correct because this
exact case is mentioned in the K & R (Last paragraph of section 7.2 in
Appendix A, page 188).  Second, this is a special case.  Sizeof, type
cast, and unary minus are all at the same precedence and they
associate right to left.  Thus the normal interpretation would be
(sizeof ((int) (-1))).  This is not the correct interpretation however
as I just mentioned.
Perry Smith
ctvax ---\
megamax --- bobkat!pedz

More information about the Comp.lang.c mailing list