tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: sizeof(type)
On Fri, Nov 29, 2024 at 09:59:19AM +0000, David Holland wrote:
> On Mon, Nov 25, 2024 at 07:53:05PM -0500, Mouse wrote:
> > There's also
> >
> > sizeof (int) - 1
> >
> > which is ambiguous even _with_ the parens; it means either
> > (sizeof(int))-1
> > or
> > sizeof((int)-1)
> >
> > In this case, I think the ambiguity is resolved by operator precedence
>
> C99 6.5.4 says
> cast-expression ::=
> unary-expression
> | ( type-name ) cast-expression
>
> and 6.5.3 says
> unary-expression ::=
> postfix-expression
> | ++ unary-expression
> | -- unary-expression
> | unary-operator cast-expression
> | sizeof unary-expression
> | sizeof ( type-name )
>
> The reason these are stratified is so that sizeof ( type-name ) is
> unambiguous.
>
> As written it also rules out ++(int)x, but that's a defect -- it is
> not semantically valid but there's no reason to gratuitously make it
> a syntax error. Especially since ++ +(int)x is accepted and that's
> semantically equally invalid.
>
It is also specified in K&R (I didn't look at the standard), p. 200,
for A7. "Expressions":
"The precedence of expressions operators is the same as the order of
the major subsections of this [A7] section, highest precedence first.
Thus, for example, the expressions referred to as the operands of +
(A7.7) are those expressions defined in A7.1--A7.6. Within each
subsection, the operators have the same precedence. [...]"
So, when considering precedence, this too has to be taken into
account.
FWIW,
--
Thierry Laronde <tlaronde +AT+ kergis +dot+ com>
http://www.kergis.com/
http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C
Home |
Main Index |
Thread Index |
Old Index