By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Precedence of sizeof

P: n/a

sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

Has it got something to do with "globbing"?

--

Frederick Gotham
Oct 6 '06 #1
Share this Question
Share on Google+
36 Replies


P: n/a


On Oct 6, 9:39 pm, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

Has it got something to do with "globbing"?

--

Frederick Gotham
"sizeof ((double)5)" works well.

Oct 6 '06 #2

P: n/a

Cong Wang wrote:
On Oct 6, 9:39 pm, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

Has it got something to do with "globbing"?

--

Frederick Gotham

"sizeof ((double)5)" works well.
Oct 6 '06 #3

P: n/a
Cong Wang posted:
"sizeof ((double)5)" works well.

Yes, I realise that. My question pertains to operator precedence and
associativity -- specifically, why:

sizeof(double)5

is interpretted by gcc as:

(sizeof(double))5

rather than:

sizeof((double)5)
--

Frederick Gotham
Oct 6 '06 #4

P: n/a
Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
sizeof has the same precedence as a cast
No it doesn't.
Has it got something to do with "globbing"?
Unlikely.

Oct 6 '06 #5

P: n/a
In article <M0*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
>sizeof has the same precedence as a cast
The standard does not describe C's syntax in terms of operator
precedence; that's just a handy way of summarizing some common cases.

The relevant part of the grammar is:

unary-expression:
[...]
unary-operator cast expression
sizeof unary-expression
sizeof ( type-name )

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

So in:
sizeof(double)5;
"(double)5" is not a unary expression, so only "sizeof ( type-name )"
is possible, and that of course doesn't work because of the "5"
afterwards.

-- Richard

Oct 6 '06 #6

P: n/a


On Oct 6, 9:39 pm, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}
The code: sizeof(double)5; in the example mean: sizeof(double) [X] 5;,
in the example the sequence [X] is none. It is trying to connect two
numeric expressions without operators. This is not correct. You may
mean some similar code like the following.

int main(void){
/*sizeof(double)5;*/
sizeof(double) * 5;
sizeof((double)5);

return 0;
}

Oct 6 '06 #7

P: n/a
On Oct 6, 9:39 am, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast
No, the sizeof operator has a higher precedence than the cast operator.
and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}
As it shouldn't because it doesn't make any sense.
Has it got something to do with "globbing"?
I have no idea what you mean by "globbing" in this context, your
expression is evaluated as:
(sizeof(double)) 5;
which is a syntax error and is why it doesn't compile.

Robert Gamble

Oct 6 '06 #8

P: n/a
Antti-Juhani Kaijanaho posted:
Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
>sizeof has the same precedence as a cast

No it doesn't.

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

--

Frederick Gotham
Oct 6 '06 #9

P: n/a
Richard Tobin posted:
unary-expression:
[...]
unary-operator cast expression
sizeof unary-expression
sizeof ( type-name )

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

So in:
> sizeof(double)5;

"(double)5" is not a unary expression, so only "sizeof ( type-name )"
is possible, and that of course doesn't work because of the "5"
afterwards.

So is a unary expression something simple like a literal or the name of an
object? Does that mean that the following should be bogus:

int main(void)
{
sizeof(5+4);
return 0;
}

It compiles just fine with gcc.

--

Frederick Gotham
Oct 6 '06 #10

P: n/a
lovecreatesbea...@gmail.com posted:
The code: sizeof(double)5; in the example mean: sizeof(double) [X] 5;,
in the example the sequence [X] is none. It is trying to connect two
numeric expressions without operators. This is not correct. You may
mean some similar code like the following.

int main(void){
/*sizeof(double)5;*/
sizeof(double) * 5;
sizeof((double)5);

return 0;
}

No. As I've already mentioned, this entire thread is to do with operator
precedence and associativity.

--

Frederick Gotham
Oct 6 '06 #11

P: n/a


On Oct 6, 9:56 pm, Frederick Gotham <fgotha...@SPAM.comwrote:
Cong Wang posted:
"sizeof ((double)5)" works well.Yes, I realise that. My question pertains to operator precedence and
associativity -- specifically, why:

sizeof(double)5

is interpretted by gcc as:

(sizeof(double))5

rather than:

sizeof((double)5)
The operand '5' has no operators on both of its left and right sides. I
think the associativity does not apply here.

Oct 6 '06 #12

P: n/a
Robert Gamble posted:
On Oct 6, 9:39 am, Frederick Gotham <fgotha...@SPAM.comwrote:
>sizeof has the same precedence as a cast

No, the sizeof operator has a higher precedence than the cast operator.
>and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

As it shouldn't because it doesn't make any sense.

It makes perfect sense. The literal, 5, is cast to a double, and the result
is supplied as an operand to sizeof.

Your expression is evaluated as: (sizeof(double)) 5; which is a syntax
error and is why it doesn't compile.

I know that. My question is why it isn't interpreted as:

sizeof((double)5)

My operator precedence table would have me to believe that it should.

--

Frederick Gotham
Oct 6 '06 #13

P: n/a
lovecreatesbea...@gmail.com posted:
> sizeof(double)5

is interpretted by gcc as:

(sizeof(double))5

rather than:

sizeof((double)5)

The operand '5' has no operators on both of its left and right sides.

5 is the operand of the cast.

The result of the cast is the operand of sizeof.

--

Frederick Gotham
Oct 6 '06 #14

P: n/a

Robert Gamble ha scritto:
No, the sizeof operator has a higher precedence than the cast operator.
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

and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

As it shouldn't because it doesn't make any sense.
Has it got something to do with "globbing"?

I have no idea what you mean by "globbing" in this context, your
expression is evaluated as:
(sizeof(double)) 5;
which is a syntax error and is why it doesn't compile.

Robert Gamble
Oct 6 '06 #15

P: n/a


On Oct 6, 10:26 pm, Frederick Gotham <fgotha...@SPAM.comwrote:
lovecreatesbea...@gmail.com posted:
sizeof(double)5
is interpretted by gcc as:
(sizeof(double))5
rather than:
sizeof((double)5)
The operand '5' has no operators on both of its left and right sides.5 is the operand of the cast.

The result of the cast is the operand of sizeof.
I meant the '5' in your original example: sizeof(double)5;

Oct 6 '06 #16

P: n/a
Frederick Gotham wrote:
lovecreatesbea...@gmail.com posted:
>> sizeof(double)5

is interpretted by gcc as:

(sizeof(double))5

rather than:

sizeof((double)5)

The operand '5' has no operators on both of its left and right sides.


5 is the operand of the cast.
The result of the cast is the operand of sizeof.
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.

--
Chris "english this isn't" Dollin
Meaning precedes definition.

Oct 6 '06 #17

P: n/a
In article <oE*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
>"(double)5" is not a unary expression, so only "sizeof ( type-name )"
is possible, and that of course doesn't work because of the "5"
afterwards.
>So is a unary expression something simple like a literal or the name of an
object?
I'm sure you can read the grammar as well as I can. But essentially:

A unary-expression is a postfix-expression optionally preceded by
++, --, &, *, +, =, ~, !, or sizeof, or is sizeof ( type-name ).

A postfix-expression is a primary-expression optionally followed by
a subscript, and argument list, ., ->, ++, or --.

A primary-expression is an identifier, a constant, a string-literal,
or a parenthesized expression.
>Does that mean that the following should be bogus:
sizeof(5+4);
No, (5+4) is a primary-expression which is a postfix-expression which is
a unary-expression.

-- Richard
Oct 6 '06 #18

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:cH*******************@news.indigo.ie...
Robert Gamble posted:
On Oct 6, 9:39 am, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast
No, the sizeof operator has a higher precedence than the cast operator.
and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}
As it shouldn't because it doesn't make any sense.


It makes perfect sense. The literal, 5, is cast to a double, and the
result
is supplied as an operand to sizeof.

Your expression is evaluated as: (sizeof(double)) 5; which is a syntax
error and is why it doesn't compile.


I know that. My question is why it isn't interpreted as:

sizeof((double)5)

My operator precedence table would have me to believe that it should.
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. They list unary operators:
sizeof,~,!,-,+,&,* as level 15 and unary casts as level 14 (i.e., cast has
lower precedence). Both are right associative. I know they corrected at
least one error in table at a later date. Whose precedence table are you
using?
Rod Pemberton
Oct 6 '06 #19

P: n/a
Frederick Gotham wrote:
Robert Gamble posted:
On Oct 6, 9:39 am, Frederick Gotham <fgotha...@SPAM.comwrote:
sizeof has the same precedence as a cast
No, the sizeof operator has a higher precedence than the cast operator.
and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}
As it shouldn't because it doesn't make any sense.


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.
Your expression is evaluated as: (sizeof(double)) 5; which is a syntax
error and is why it doesn't compile.


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

Oct 6 '06 #20

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:M0*******************@news.indigo.ie...
>
sizeof has the same precedence as a cast, and they both bind from right to
left. The following won't compile for me with gcc:

int main(void)
{
sizeof(double)5;

return 0;
}

Has it got something to do with "globbing"?

--

Frederick Gotham
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

This ambiguous case is resolved quite arbitrarily by declaring that the
latter interpretation shall be used.

Now replace the minus with a plus:
sizeof(double)+5
By the same logic, this is interpreted as (sizeof(double)) + 5

The same logic is used to have
sizeof(double)5 interpreted as (sizeof(double))5
which is a syntax error.

This type of thing is why many people like to
include parentheses around the operand of sizeof regardless of
whether that operand is a type name or a data object. It makes it
clear what the intent of the expression really is.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 6 '06 #21

P: n/a
In article <eg**********@main.corriga.net>,
Rod Pemberton <do*********@bitfoad.cmmwrote:
>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
Oct 6 '06 #22

P: n/a
Frederick Gotham wrote:
Antti-Juhani Kaijanaho posted:
Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
sizeof has the same precedence as a cast
No it doesn't.

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

Oct 6 '06 #23

P: n/a
Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
Antti-Juhani Kaijanaho posted:
>Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
>>sizeof has the same precedence as a cast

No it doesn't.


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.

Oct 6 '06 #24

P: n/a
xdevel <xd********@yahoo.comkirjoitti 06.10.2006:
sizeof and cast have the same precedence in fact they are on
the same row:
! ~ ++ -- + - * & (type) sizeof
but they bind from right to left
C&V please.

Oct 6 '06 #25

P: n/a
xdevel wrote:
Robert Gamble ha scritto:
No, the sizeof operator has a higher precedence than the cast operator.

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

Oct 6 '06 #26

P: n/a
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.

--

Frederick Gotham
Oct 6 '06 #27

P: n/a
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?

--

Frederick Gotham
Oct 6 '06 #28

P: n/a
Frederick Gotham wrote:
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.
It doesn't parse using the grammar given by the C standard. The C
standard defines C.

Oct 6 '06 #29

P: n/a
Frederick Gotham wrote:
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?
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....3?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."

Oct 6 '06 #30

P: n/a
In article <oE*******************@news.indigo.ieFrederick Gotham <fg*******@SPAM.comwrites:
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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 6 '06 #31

P: n/a
Frederick Gotham <fg*******@SPAM.comkirjoitti 06.10.2006:
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:)

Oct 7 '06 #32

P: n/a
sj*******@yahoo.com posted:
This post might be enlightening:
http://groups.google.com/group/comp....ec23?dmode=sou
rce

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.

--

Frederick Gotham
Oct 7 '06 #33

P: n/a
In article <qM*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
>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
Oct 7 '06 #34

P: n/a
Frederick Gotham wrote:
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?
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".

Oct 9 '06 #35

P: n/a
Frederick Gotham wrote:
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?
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.

--
Chris "falling further in" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Oct 9 '06 #36

P: n/a

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

Oct 9 '06 #37

This discussion thread is closed

Replies have been disabled for this discussion.