473,513 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2999
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
16546
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
9447
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
4943
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
412
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
2297
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
2588
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 ?
0
7161
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
7384
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,...
0
7539
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...
1
7101
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...
1
5089
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...
0
4746
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...
0
3234
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
456
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...

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.