473,287 Members | 1,463 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,287 software developers and data experts.

sizeof (char)(char)2

This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical precedence
and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.
Nov 14 '05 #1
9 2979
M Welinder wrote on 26/07/04 :
This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical precedence
and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.


Because the type must be enclosed in parenthesis when used with sizeof.

The correct syntax is:

sizeof object
sizeof (type)

sizeof (type) (type)

is not correct C.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
te***@gnu.org (M Welinder) writes:
This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical precedence
and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.


The C standard does not use the concept of precedence, but achieves the
same effect through the use of syntax rules. In terms of precedence,
the `sizeof' operator has higher precedence than the cast operator, i.e.

sizeof (char)(char)2

behaves like

(sizeof (char)) (char)2

which is indeed a syntax error.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
Emmanuel Delahaye wrote:
M Welinder wrote on 26/07/04 :
This doesn't work with any C compiler that I can find. They all
report a syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical
precedence and right-to-left parsing, so why isn't the above
equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.
Because the type must be enclosed in parenthesis when used with sizeof.

The correct syntax is:

sizeof object
sizeof (type)


Right.
sizeof (type) (type)

is not correct C.


Maybe. Firstly, this discussion is purely academic; anybody writing such
code should be LARTed big time.
Now, about the example, I think that it mixes two things:
1. double cast
2. applying sizeof on the result of that cast

What I think is that (provided the precedence the OP claims is really
required) '(char)(char)2' should be evaluated first, and that is in my
understanding an object, which is why the 'sizeof object' syntax applies.

Now, to the OP: which of the above two things that you mixed are really
bothering you? Or is it really the combination that you can't get past any
compiler?

Uli

Nov 14 '05 #4
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote in message news:<mn***********************@YOURBRAnoos.fr>...
Because the type must be enclosed in parenthesis when used with sizeof.

The correct syntax is:

sizeof object
sizeof (type)


That misses the point.

Notice that these _are_ valid:

printf ("%d\n", (int)sizeof +(char)(char)2);
printf ("%d\n", (int)sizeof ((char)(char)2));

So to rephrase: why isn't "sizeof (char)(char)2)" parsed as the latter?
It fits "sizeof object" nicely, but it feels like there is some unstated
"dont' do anything an LR(1) parser couldn't handle" rule.

Or even more explictly: what in the C definition precludes
"sizeof (char)(char)2)" being parsed as this?

sizeof
|
typecast-expr
| |
"char" |
typecast-expr
| |
"char" |
int-constant
|
2
Nov 14 '05 #5

"M Welinder" <te***@gnu.org> wrote in message
news:64**************************@posting.google.c om...
This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.


It has nothing to do with precedence. The C standard says that "sizeof"
followed by a left parenthesis should be parsed as size of type expression.
This is one reason blindly relying on a precedence table is bad. I remember
reading an article on this once in one of the journals. Many wrote in
refusing to believe it. Many of them kept parrots the same crap about
precendence.

Nov 14 '05 #6
M Welinder wrote:
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote in message news:<mn***********************@YOURBRAnoos.fr>...

Because the type must be enclosed in parenthesis when used with sizeof.

The correct syntax is:

sizeof object
sizeof (type)

That misses the point.

Notice that these _are_ valid:

printf ("%d\n", (int)sizeof +(char)(char)2);
printf ("%d\n", (int)sizeof ((char)(char)2));

So to rephrase: why isn't "sizeof (char)(char)2)" parsed as the latter?
It fits "sizeof object" nicely, but it feels like there is some unstated
"dont' do anything an LR(1) parser couldn't handle" rule.

Or even more explictly: what in the C definition precludes
"sizeof (char)(char)2)" being parsed as this?

sizeof
|
typecast-expr
| |
"char" |
typecast-expr
| |
"char" |
int-constant
|
2


Um, er, "the grammar in the Standard" is what forbids
such a parse. The directly relevant pieces are in 6.5.3
(some forms omitted for brevity):

unary-expression:
postfix-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

and 6.5.4:

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

`sizeof +(char)(char)2' works because `+(char)(char)2'
is a "unary-expression:" it matches the second form in the
above (abbreviated) 6.5.3 list. `sizeof (char)(char)2' fails
because `(char)(char)2' is not a "unary-expression" but a
"cast-expression." (You could observe the same effect with
the shorter `sizeof (short)42'.)

Informally, `sizeof' has higher precedence than casts,
despite what K&R say (in the first edition, anyhow; I don't
have K&R II).

--
Er*********@sun.com

Nov 14 '05 #7
Xenos wrote:
"M Welinder" <te***@gnu.org> wrote in message
news:64**************************@posting.google.c om...
This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical


precedence
and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));

It should probably be obvious to me, but right now it isn't.

It has nothing to do with precedence. The C standard says that "sizeof"
followed by a left parenthesis should be parsed as size of type expression.


No, it does not -- in fact, the last line of code you
quoted provides a counter-example.

--
Er*********@sun.com

Nov 14 '05 #8
te***@gnu.org (M Welinder) wrote:
This doesn't work with any C compiler that I can find. They all report a
syntax error:

printf ("%d\n", (int)sizeof (char)(char)2);

Now the question is "why?" "sizeof" and "(char)" have identical precedence
and right-to-left parsing, so why isn't the above equivalent to

printf ("%d\n", (int)sizeof ((char)(char)2));


Because C syntax is defined by a BNF grammar, and the grammar says so.
Any notions of "precedence" and "right-to-left parsing" are just convenient
approximations that humans can digest more easily than BNF, and as this
example shows, aren't always reliable.

Also, don't be too surprised if this example works in C++, as there
are minor differences in their grammars, even for the 'common subset'
of the languages.
Nov 14 '05 #9
In message <41**************@sun.com>
Eric Sosman <Er*********@sun.com> wrote:
Um, er, "the grammar in the Standard" is what forbids
such a parse. The directly relevant pieces are in 6.5.3
(some forms omitted for brevity):

unary-expression:
postfix-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

and 6.5.4:

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

`sizeof +(char)(char)2' works because `+(char)(char)2'
is a "unary-expression:" it matches the second form in the
above (abbreviated) 6.5.3 list. `sizeof (char)(char)2' fails
because `(char)(char)2' is not a "unary-expression" but a
"cast-expression." (You could observe the same effect with
the shorter `sizeof (short)42'.)

Informally, `sizeof' has higher precedence than casts,
despite what K&R say (in the first edition, anyhow; I don't
have K&R II).


In a related vein, I found a nasty in our compiler recently - it was
incorrectly parsing

sizeof (char[]) { "Foo" }

Anyone implementing C99, particularly when adding it to an existing C90
compiler, should check that they handle that correctly; our parser required
some additional code in sizeof to test for the presence of a '{' after (
type-name ).

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #10

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

Similar topics

5
by: nsgi_2004 | last post by:
I have a C book that says the sizeof a char is one byte. Is this true for C++?
17
by: José de Paula | last post by:
Is it a given that sizeof(char) always yields 1, no matter the implementation? I ask because I saw some critics against char * ptr = malloc (sizeof(char) * NUM); in favour of simply char * ptr...
9
by: Sunner Sun | last post by:
Hi, all FAQ of comp.lang.c said "In C, type char is defined as occupying one byte, so it is usually 8 bits". (http://www.eskimo.com/~scs/cclass/notes/sx2a.html) But I found in C99 that "An...
0
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
1
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); ...
2
by: arnaudk | last post by:
Why is sizeof(char*) = 4? I would have expected 1... Is it because a pointer is stored as an int ?
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.