473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Precedence of sizeof


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
36 2753


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

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


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


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

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


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

"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
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

"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet....
11
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
7
by: lovecreatesbea... | last post by:
c-faq, 4.3: The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. BSD Manual, OPERATOR(7): Operator Associativity...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
7
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.