473,698 Members | 2,047 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 #1
36 2794


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.in digo.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
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 #9
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 #10

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

Similar topics

47
2676
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
1549
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
1786
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
3745
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
1950
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
3306
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
8674
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
8603
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
9157
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...
1
8893
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
7723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
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.