473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
36 2801
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 #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.in digo.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******** ***********@new s.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
2691
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. struct NodeT { Lnk_T Lnk ; };
11
1553
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
1787
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 -------- ------------- ! ~ ++ -- - (type) * & sizeof right to left Is there a complete table on operators, their precedence and
9
3746
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'); printf("%d", a); /* code end */ 2
7
1956
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 think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are higher than the third and all three operators are
32
3314
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 += i + j + k++;
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.