473,725 Members | 2,251 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

"Frederick Gotham" <fg*******@SPAM .comwrote in message
news:M0******** ***********@new s.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**********@m ain.corriga.net >,
Rod Pemberton <do*********@bi tfoad.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********@yah oo.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

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
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.