473,320 Members | 2,041 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,320 software developers and data experts.

sizeof(x)

In C99 it is mentioned:

"The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.".
If I am not wrong, this implies that

int x;

size_t y= sizeof(x);
is not valid.
and only the following is valid:
int x;

size_t y= sizeof x;
However I am puzzled, and thought the first was also valid in
C90/C95(/C++03).

Mar 30 '08 #1
12 2713
On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
In C99 it is mentioned:

"The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.".

If I am not wrong, this implies that

int x;

size_t y= sizeof(x);
is not valid.
You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).
Mar 30 '08 #2
Harald van Dijk wrote:
On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
>In C99 it is mentioned:

"The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.".

If I am not wrong, this implies that

int x;

size_t y= sizeof(x);
is not valid.

You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).

I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Mar 30 '08 #3
Ioannis Vranos wrote:
>
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"
Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Mar 30 '08 #4
Ioannis Vranos wrote:
Ioannis Vranos wrote:
>I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.


More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

--
Ian Collins.
Mar 30 '08 #5
Ian Collins wrote:
Ioannis Vranos wrote:
>Ioannis Vranos wrote:
>>I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?
.... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.

Mar 30 '08 #6
On Sun, 30 Mar 2008 23:07:05 +0300, Ioannis Vranos wrote:
Harald van Dijk wrote:
>On Sun, 30 Mar 2008 22:50:00 +0300, Ioannis Vranos wrote:
>>In C99 it is mentioned:

"The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.".

If I am not wrong, this implies that

int x;

size_t y= sizeof(x);
is not valid.

You are; (x) is a perfectly valid expression, so there's no problem
taking the size of (x).

I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
That doesn't say sizeof(x) is invalid any more than the standard does.
Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
Yes, it does. Look at the grammar.

unary-expression:
sizeof unary-expression

unary-expression:
postfix-expression

postfix-expression:
primary-expression

primary-expression:
( expression )

A parenthesised expression is a primary-expression, which is a postfix-
expression, which is a unary-expression, which is a valid operand of
sizeof.

The standard doesn't explicitly mention that parenthesised expressions
are valid operands of +, -, *, /, ^, &, or pretty much any other
operator. The grammar makes that clear already.
Mar 30 '08 #7
On Sun, 30 Mar 2008 23:11:55 +0300, Ioannis Vranos wrote:
Ian Collins wrote:
>Ioannis Vranos wrote:
>>Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
sizeofx doesn't compile because there's no sizeof operator. There's a
sizeofx identifier. sizeof(x) does and should compile because sizeof( is
not a valid identifier. This is the same reason why a+++++b is not valid,
but a+++--b is: the first is split into {a}{++}{++}{+}{b}, and the second
is split into {a}{++}{+}{--}{b}.
Mar 30 '08 #8
Ioannis Vranos wrote:
Ian Collins wrote:
>Ioannis Vranos wrote:
>>Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
Try sizeof(((((((((((((((((((((x)))))))))))))))))))))
and perhaps the answer will come to you.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Mar 30 '08 #9
Eric Sosman wrote:
Ioannis Vranos wrote:
>Ian Collins wrote:
>>Ioannis Vranos wrote:
Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.
More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"
Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.

Try sizeof(((((((((((((((((((((x)))))))))))))))))))))
and perhaps the answer will come to you.

Yes, it had already arrived. :-) Thanks anyway.

Mar 30 '08 #10
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrites:
Ian Collins wrote:
>Ioannis Vranos wrote:
>>Ioannis Vranos wrote:
I first saw that only sizeof x is valid at the pdf hosted at
http://cprog.tomsweb.net.

More specifically the above writes:

"sizeof Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression"

Did you read what Harald said: "(x) is a perfectly valid expression"?

... right. However sizeofx doesn't compile and if (x) was considered an
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
C code is split into tokens before those tokens are parsed. (The
process actually involves "preprocessor tokens", which are later
converted to "tokens", but that doesn't matter in this case.)

``sizeof'' is a keyword; like all keywords, it has the form of an
identifier. ``('' and ``)'' are punctuators. Two adjacent
identifiers, keywords, or numeric constants must be separated by
whitespace (a comment counts as whitespace). A keyword and a
punctuator don't need any whitespace to separate them.

``sizeofx'' is just a single identifier that has nothing to do with
the ``sizeof'' keyword. It compiles just fine if you happen to have
declared it:

int sizeofx = 42;
sizeofx;

``sizeof x'' is two tokens, ``sizeof'' and ``x''. If ``x'' is an
expression (actually a unary-expression; see the grammar), then that's
a legal expression.

``sizeof(x)'' is four tokens, ``sizeof'', ``('', ``x'', and ``)''. If
``x'' is a type-name, then that's a valid expression. If ``x'' is an
expression, then ``(x)'' is also a valid expression, and the whole
thing is also a valid expression.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 30 '08 #11
On Mar 30, 1:11*pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
wrote:
expression it should be sizeof (x), and sizeof(x) shouldn't compile.
Good grief!

Do you realize that children that were in junior highschool back when
you first started being a goof here now have CS degrees and software
jobs?

You need to disable your anti-learning filter, or at least reduce the
coefficients in its confusion matrix a little bit.
Mar 30 '08 #12
In article <fs***********@ulysses.noc.ntua.gr>,
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrote:
>Then I checked the C99 standard and it mentions what is shown above.
Clearly C99 doesn't mention parenthesized expression.
A parenthesized expression is an expression, (that's a fact about C
grammar, not the English language). sizeof(x) is no more a problem
than a[(x)].

-- Richard
--
:wq
Mar 31 '08 #13

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

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
9
by: M Welinder | last post by:
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...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.