Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding sizeof Operator

Reply
Thread Tools

Regarding sizeof Operator

 
 
sonu
Guest
Posts: n/a
 
      03-14-2006
#include<stdio.h>
main()
{
int x=10,y;
y=sizeof(++x);

printf("x=%d\ny=%d\n",x,y);
}

Oput Put

x=10
y=4

why not x=11

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-14-2006
sonu said:

> #include<stdio.h>
> main()
> {
> int x=10,y;
> y=sizeof(++x);
>
> printf("x=%d\ny=%d\n",x,y);
> }
>
> Oput Put
>
> x=10
> y=4
>
> why not x=11


Because sizeof does not evaluate its operand.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-14-2006
On Tuesday 14 March 2006 07:10, sonu opined (in
<(E-Mail Removed) .com>):

> #include<stdio.h>
> main()
> {
> int x=10,y;
> y=sizeof(++x);
>
> printf("x=%d\ny=%d\n",x,y);
> }
>
> Oput Put
>
> x=10
> y=4
>
> why not x=11


Do not post the same question every 6 minutes, especially not with the
different subject line (it makes it difficult to ignore). It is only
reasonable to assume your post is not seen after at least 24 hours.
This is not a chat room, this is Usenet. Look up what it is and it's
history. GIYF.

In any case, you did get two replies very quickly.

--
BR, Vladimir

Latin is a language,
As dead as can be.
First it killed the Romans,
And now it's killing me.

 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, sonu <(E-Mail Removed)> wrote:
> #include<stdio.h>
> main()
> {
> int x=10,y;
> y=sizeof(++x);
>
> printf("x=%d\ny=%d\n",x,y);
> }
>
> Oput Put
>
> x=10
> y=4
>
> why not x=11
>


"sizeof" operator does not evaluate the expression contained therein
it seems. I didnt know that : but purely because I never stuck an
expression in it for reasons unknown - I just assumed it as a
"preprocessor" type thing and just never did it. I can claim no genius.

It would be the same had you put

sizeof(x=3);

I would agree that it is a little confusing since the
compiler (gcc) happily lets you write something like

sizeof(x=funcCall(y)) too!

Looking at this in n1124pdf:

6.5.3.4 The sizeof operator
Constraints

2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size is
determined from the type of the operand. The result is an integer. If
the type of the operand is a variable length array type, the operand
is evaluated; otherwise, the operand is not evaluated and the result
is ....

There is a rather unhelpful footnote which states

"the operand of a sizeof operator is usually not evaluated"

Hopefully someone will enlighten us both as to what the standard means
: I just assumed it was a pre-processor type thing and would never
have thought the compiler would allow such a statement there.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-14-2006
Richard G. Riley said:

> "sizeof" operator does not evaluate the expression contained therein
> it seems.


Correct.

> I didnt know that :


Evidently.

> but purely because I never stuck an
> expression in it for reasons unknown - I just assumed it as a
> "preprocessor" type thing and just never did it.


It is nothing to do with the preprocessor.


> I can claim no genius.


Evidently.

>
> It would be the same had you put
>
> sizeof(x=3);


Correct.

>
> I would agree that it is a little confusing


I wouldn't.

> since the
> compiler (gcc) happily lets you write something like
>
> sizeof(x=funcCall(y)) too!


It's supposed to.

>
> Looking at this in n1124pdf:
>
> 6.5.3.4 The sizeof operator
> Constraints
>
> 2 The sizeof operator yields the size (in bytes) of its operand, which
> may be an expression or the parenthesized name of a type. The size is
> determined from the type of the operand. The result is an integer. If
> the type of the operand is a variable length array type, the operand
> is evaluated; otherwise, the operand is not evaluated and the result
> is ....
>
> There is a rather unhelpful footnote which states
>
> "the operand of a sizeof operator is usually not evaluated"


That is merely an expansion of the normative text. It adds no particular
value.

> Hopefully someone will enlighten us both as to what the standard means
> : I just assumed it was a pre-processor type thing


sizeof is a C keyword, not a preprocessor directive.

> and would never
> have thought the compiler would allow such a statement there.


Evidently. And yet it does, because it must.

What you think does not necessarily equate to what is true.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-14-2006
Richard Heathfield wrote:
> Richard G. Riley said:
>



<snip>

>> There is a rather unhelpful footnote which states
>>
>> "the operand of a sizeof operator is usually not evaluated"

>
> That is merely an expansion of the normative text. It adds no
> particular value.


Although sizeof must evaluate the size of a variable length array under c99
rules ... maybe it's a vague reference to that - I believe it's the only
case in which sizeof /evaluates/?

<snip>


--
==============
Not a pedant
==============


 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, pemo <(E-Mail Removed)> wrote:
> Richard Heathfield wrote:
>> Richard G. Riley said:
>>

>
>
><snip>
>
>>> There is a rather unhelpful footnote which states
>>>
>>> "the operand of a sizeof operator is usually not evaluated"

>>
>> That is merely an expansion of the normative text. It adds no
>> particular value.

>
> Although sizeof must evaluate the size of a variable length array under c99
> rules ... maybe it's a vague reference to that - I believe it's the only
> case in which sizeof /evaluates/?
>
><snip>
>
>


I just found the relevant part in K&R2 A7.4.8 : it states that expressions
are not evaluated. While I did download n1124.pdf, I always find it
hard to find something there and all I did find I documented in
that post. I'm sure KT or somewould more aufait with the document can
quote the relevant part.


--
Debuggers : you know it makes sense.
http://heather.cs.ucdavis.edu/~matlo...g.html#tth_sEc
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      03-14-2006
Richard G. Riley wrote:
> On 2006-03-14, pemo <(E-Mail Removed)> wrote:
> > Richard Heathfield wrote:
> >> Richard G. Riley said:
> >>

> >
> >
> ><snip>
> >
> >>> There is a rather unhelpful footnote which states
> >>>
> >>> "the operand of a sizeof operator is usually not evaluated"
> >>
> >> That is merely an expansion of the normative text. It adds no
> >> particular value.

> >
> > Although sizeof must evaluate the size of a variable length array under c99
> > rules ... maybe it's a vague reference to that - I believe it's the only
> > case in which sizeof /evaluates/?
> >
> ><snip>
> >
> >

>
> I just found the relevant part in K&R2 A7.4.8 : it states that expressions
> are not evaluated.


They weren't when K&R2 was published. K&R2 doesn't cover C99.

> While I did download n1124.pdf, I always find it
> hard to find something there and all I did find I documented in
> that post. I'm sure KT or somewould more aufait with the document can
> quote the relevant part.


(It's "au fait" by the way, two words)

Did you even read the section you posted?

"If the type of the operand is a variable length array type, the
operand
is evaluated; otherwise, the operand is not evaluated ..."

What isn't clear about that?

Robert Gamble

 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, Robert Gamble <(E-Mail Removed)> wrote:
> Richard G. Riley wrote:
>> On 2006-03-14, pemo <(E-Mail Removed)> wrote:
>> > Richard Heathfield wrote:
>> >> Richard G. Riley said:
>> >>
>> >
>> >
>> ><snip>
>> >
>> >>> There is a rather unhelpful footnote which states
>> >>>
>> >>> "the operand of a sizeof operator is usually not evaluated"
>> >>
>> >> That is merely an expansion of the normative text. It adds no
>> >> particular value.
>> >
>> > Although sizeof must evaluate the size of a variable length array under c99
>> > rules ... maybe it's a vague reference to that - I believe it's the only
>> > case in which sizeof /evaluates/?
>> >
>> ><snip>
>> >
>> >

>>
>> I just found the relevant part in K&R2 A7.4.8 : it states that expressions
>> are not evaluated.

>
> They weren't when K&R2 was published. K&R2 doesn't cover C99.
>
>> While I did download n1124.pdf, I always find it
>> hard to find something there and all I did find I documented in
>> that post. I'm sure KT or somewould more aufait with the document can
>> quote the relevant part.

>
> (It's "au fait" by the way, two words)
>
> Did you even read the section you posted?


<sarcasm on> No. I posted it totally blindly just guessing it had
something to do with the OP. I guess I was luck I hit on something to
do with sizeof eh? <sarcasm off>


>
> "If the type of the operand is a variable length array type, the
> operand
> is evaluated; otherwise, the operand is not evaluated ..."
>
> What isn't clear about that?


The footnote I also cited?

"the operand of a sizeof operator is usually not evaluated"

As I said, I'm not so au fait with this stuff and sometimes have
difficulty translating it too, and the word "usually" left some doubt
in my mind.

I did state that I wasnt sure? Didnt I?

But thanks for the clarification anyway.

>
> Robert Gamble
>

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-14-2006
pemo said:

> Richard Heathfield wrote:
>> Richard G. Riley said:
>>

>
>
> <snip>
>
>>> There is a rather unhelpful footnote which states
>>>
>>> "the operand of a sizeof operator is usually not evaluated"

>>
>> That is merely an expansion of the normative text. It adds no
>> particular value.

>
> Although sizeof must evaluate the size of a variable length array under
> c99 rules ...


Yes, and the normative text explains that perfectly clearly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
regarding sizeof operator for class with no data variables pratap C++ 3 08-20-2007 08:18 AM
regarding sizeof operator for class with no data variables pratap C++ 1 08-20-2007 04:23 AM
regarding sizeof operator for class with no data variables pratap C++ 0 08-19-2007 06:25 PM
Regarding sizeof operator venkat C Programming 3 03-05-2007 01:10 PM



Advertisments