sizeof(x)

I

Ioannis Vranos

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).
 
H

Harald van Dijk

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

Ioannis Vranos

Harald said:
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.
 
I

Ian Collins

Ioannis said:
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"?
 
I

Ioannis Vranos

Ian said:
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.
 
H

Harald van Dijk

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.
 
H

Harald van Dijk

... 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}.
 
E

Eric Sosman

Ioannis said:
... 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.
 
K

Keith Thompson

Ioannis Vranos said:
... 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.
 
K

Kaz Kylheku

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.
 
R

Richard Tobin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top