Precedence of sizeof

  • Thread starter Frederick Gotham
  • Start date
R

Robert Gamble

Frederick said:
Robert Gamble posted:



It makes perfect sense.

To you it does, that's the problem.
The literal, 5, is cast to a double, and the result
is supplied as an operand to sizeof.

I have already explained to you that this is not the case since the
sizeof operator has a higher precedence than a cast, repeating your
assertion will do little to change the facts.
I know that. My question is why it isn't interpreted as:

sizeof((double)5)

And I have clearly explained why.
My operator precedence table would have me to believe that it should.

Either you or your operator table needs to be tweaked.

Robert Gamble
 
R

Richard Tobin

Rod Pemberton said:
I'm not sure how they calculate the precedence level from the grammar, but
"C: A Reference Manual" by Samuel Harbison and Guy Steele, Jr. 3rd. edition,
page 167, lists 17 precedence levels.

Presumably it just corresponds to the hierarchy of 17 productions from
"expression" down to "postfix-expression".

I always found it a little strange to talk about predence levels for
unary operators. For binary operators and unary operators on opposite
sides, precedence tells you which operator binds most tightly. But
for unary operators on the same side, it just tells you which
combinations are legal.

-- Richard
 
R

Robert Gamble

Frederick said:
Antti-Juhani Kaijanaho posted:


Please indicate where you obtain that information, because my own operator
table indicates that they indeed have equal precedence.

It's clearly spelled out in the Standard, section 6.5 for C99, section
3.3 for ANSI C89.

Robert Gamble
 
A

Antti-Juhani Kaijanaho

Frederick Gotham said:
Antti-Juhani Kaijanaho posted:



Please indicate where you obtain that information, because my own operator
table indicates that they indeed have equal precedence.

Where in the standard is that precedence table? My copies do not have
it. Instead, it defines the grammar as follows:

6.5.3:
unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

unary-operator: one of
& * + - ~ !

6.5.4:
cast-expression:
unary-expression
( type-name ) cast-expression

It is quite clear from these grammar excerpts that casts and sizeof do not
have the same precedence; sizeof binds more tightly.

To use a cast expression in the sizeof operand, it needs to be put in
parentheses.
 
R

Robert Gamble

xdevel said:
Robert Gamble ha scritto:


No, sizeof and cast have the same precedence in fact they are on
the same row:
! ~ ++ -- + - * & (type) sizeof
but they bind from right to left

In the same row of what? Your precedence table? You do realize that
it is the Standard that dictates the precedence of operators, not
whatever table you are looking at don't you?

Robert Gamble
 
F

Frederick Gotham

Chris Dollin posted:
Since the text doesn't parse, you can't say "5 is the operand of the cast"
or "The result of the cast is the operand of sizeof" and be talking
about C grammar.


Who knows if it doesn't parse? All I said was that gcc didn't compile it. gcc
isn't C.
 
F

Frederick Gotham

Fred Kleinschmidt posted:
Consider this:
sizeof(double)-5

There is a syntactic ambiguity in this type of expression. It could be
interpreted as three unary operators: negation, the cast (double)
and 'sizeof', successively applied to the value 5:
sizeof( (double)(-5) )
or it could be interpreted as a binaary subtraction whose
operands are sizeof(double) and 5:
(sizeof (double)) - 5


Shouldn't an operator table clarify this though, indicating what's evaluated
in what order?
 
G

Guest

Frederick said:
Chris Dollin posted:

Who knows if it doesn't parse? All I said was that gcc didn't compile it. gcc
isn't C.

It doesn't parse using the grammar given by the C standard. The C
standard defines C.
 
S

sjdevnull

Frederick said:
Fred Kleinschmidt posted:



Shouldn't an operator table clarify this though, indicating what's evaluated
in what order?

I don't know what you mean by "operator table", but the grammar does
clarify this.

This post might be enlightening:
http://groups.google.com/group/comp.lang.c/msg/6e74936978cfec23?dmode=source

I found this particularly helpful:

"K&R (either edition), and most sensible humans, use an "operator
precedence" grammar to describe expressions in C. The C standards (C89
and C99 both) instead use a fully factored grammar, in which the
problems solved by "operator precedence and associativity" never even
arise."
 
D

Dik T. Winter

> Richard Tobin posted:
> > unary-expression:
> > [...]
> > unary-operator cast expression
> > sizeof unary-expression
> > sizeof ( type-name )
....
> So is a unary expression something simple like a literal or the name of an
> object?

Richard skipped something. In addition to what he mentioned, it can
also be a "postfix-expression", and going further down the syntax we find that
(1) a postfix-expression can be a "primary-expression",
(2) a primary-expression can be:
identifier
constant
string-literal
( expression )
> int main(void)
> {
> sizeof(5+4);
> return 0;
> }
>
> It compiles just fine with gcc.

It should.
 
A

Antti-Juhani Kaijanaho

Frederick Gotham said:
Shouldn't an operator table clarify this though, indicating what's evaluated
in what order?

Operator precedence does not specify evaluation order; it is only used
to disambiguate ambiguous expressions. (Of course, in standard C, there
are no ambiguous expressions:)
 
F

Frederick Gotham

(e-mail address removed) posted:


An absolute masterpiece of a post! I quite like:

| This, incidentally, is also why parentheses are
| not required around "b = c" in:
|
| result = (a ? b = c : d);


Still though, the following expression:

sizeof(double)-5

has an ambiguous parse, and so operator precedence should come into play --
which would lead me to believe that there is a cast involved.
 
R

Richard Tobin

Frederick Gotham said:
Still though, the following expression:

sizeof(double)-5

has an ambiguous parse

It does not have an ambiguous parse according to the grammar in the
standard, because (double)-5 is not a unary expression and therefore
cannot be the argument to the argument to the sizeof operator.

-- Richard
 
O

Old Wolf

Frederick said:
Fred Kleinschmidt posted:


Shouldn't an operator table clarify this though, indicating what's evaluated
in what order?

Maybe you could address your concerns to whoever wrote
this "operator table" that you keep going on about.

The C standard does not define an "operator table" or "precedence".
 
C

Chris Dollin

Frederick said:
Chris Dollin posted:


Who knows if it doesn't parse?

Someone who reads the C grammar.
All I said was that gcc didn't compile it. gcc isn't C.

Even if gcc accepted the expression "batman @prescribes valium"
and compiled code to teleport tribbles to Gotham city, it wouldn't
cause the C grammar to suddenly accept as an expression the text
you've been quoting.
 
X

xdevel

Robert Gamble ha scritto:
You do realize that
it is the Standard that dictates the precedence of operators, not
whatever table you are looking at don't you?

I read it, simply, from K&R and other C/C++ books
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top