[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

a basic question,need help

jackie

8/13/2008 2:57:00 AM

i know that in c plus plus,++x returns l-value while x++ returns r-
value,but what is the situation in c,are both ++x and x++ return r-
value? i don't know how C99 defines it,thx.
17 Answers

Eric Sosman

8/13/2008 3:25:00 AM

0

jackie wrote:
> i know that in c plus plus,++x returns l-value while x++ returns r-
> value,but what is the situation in c,are both ++x and x++ return r-
> value? i don't know how C99 defines it,thx.

In C (all versions), neither ++x nor x++ is an lvalue.
I'm surprised to hear that this is different in C++, because
it makes no sense to me. Is

int x = 42;
++x = 97;

legal in C++? If so, what is the value of x afterwards?
(Actually, you can disregard the second question: If the
answer to the first is anything other than "No," I don't
want to know anything more about C++.)

--
Eric Sosman
esosman@ieee-dot-org.invalid

Tommy

8/13/2008 3:49:00 AM

0

[cuitao@mytest c]$ vi pp.c

#include <stdio.h>
int main()
{
int x=10;
x=20;
printf("\nx=%d\n",x);

return 0;
}
~
~
~
"pp.c" 9L, 99C written
[cuitao@mytest c]$
[cuitao@mytest c]$
[cuitao@mytest c]$ cc ./pp.c -o ./pp
[cuitao@mytest c]$
[cuitao@mytest c]$ ./pp

x=20
[cuitao@mytest c]$


[cuitao@mytest c]$ vi pp.c

#include <stdio.h>
int main()
{
int x=10;
x++=20;
printf("\nx=%d\n",x);

return 0;
}
~
~
~
"pp.c" 9L, 101C written
[cuitao@mytest c]$
[cuitao@mytest c]$
[cuitao@mytest c]$ cc ./pp.c -o ./pp
../pp.c: In function `main':
../pp.c:5: error: invalid lvalue in assignment





[cuitao@mytest c]$ vi pp.c

#include <stdio.h>
int main()
{
int x=10;
++x=20;
printf("\nx=%d\n",x);

return 0;
}
~
~
~
"pp.c" 9L, 101C written
[cuitao@mytest c]$ cc ./pp.c -o ./pp
../pp.c: In function `main':
../pp.c:5: error: invalid lvalue in assignment
[cuitao@mytest c]$







"Eric Sosman" <esosman@ieee-dot-org.invalid>
??????:4sadnefSS4WSyT_VnZ2dnUVZ_jOdnZ2d@comcast.com...
> jackie wrote:
>> i know that in c plus plus,++x returns l-value while x++ returns r-
>> value,but what is the situation in c,are both ++x and x++ return r-
>> value? i don't know how C99 defines it,thx.
>
> In C (all versions), neither ++x nor x++ is an lvalue.
> I'm surprised to hear that this is different in C++, because
> it makes no sense to me. Is
>
> int x = 42;
> ++x = 97;
>
> legal in C++? If so, what is the value of x afterwards?
> (Actually, you can disregard the second question: If the
> answer to the first is anything other than "No," I don't
> want to know anything more about C++.)
>
> --
> Eric Sosman
> esosman@ieee-dot-org.invalid


Ian Collins

8/13/2008 4:00:00 AM

0

Eric Sosman wrote:
> jackie wrote:
>> i know that in c plus plus,++x returns l-value while x++ returns r-
>> value,but what is the situation in c,are both ++x and x++ return r-
>> value? i don't know how C99 defines it,thx.
>
> In C (all versions), neither ++x nor x++ is an lvalue.
> I'm surprised to hear that this is different in C++, because
> it makes no sense to me. Is
>
> int x = 42;
> ++x = 97;
>
> legal in C++?

No.

--
Ian Collins.

CBFalconer

8/13/2008 4:00:00 AM

0

jackie wrote:
>
> i know that in c plus plus,++x returns l-value while x++ returns
> r- value,but what is the situation in c,are both ++x and x++
> return r- value? i don't know how C99 defines it,thx.

You're wrong. Illustrative snippet:

volatile int x = 123;
int y;

y = x;
if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
else puts("Bad system");

y = x;
if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
else puts("Bad system");

The operator has nothing to to with lvalues and rvalues. Same in
C++. Note that in the snippet the first phrase in the test (before
the &&) is completed before the 2nd phase is begun.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.

Daniel Pitts

8/13/2008 4:38:00 AM

0

Ian Collins wrote:
> Eric Sosman wrote:
>> jackie wrote:
>>> i know that in c plus plus,++x returns l-value while x++ returns r-
>>> value,but what is the situation in c,are both ++x and x++ return r-
>>> value? i don't know how C99 defines it,thx.
>> In C (all versions), neither ++x nor x++ is an lvalue.
>> I'm surprised to hear that this is different in C++, because
>> it makes no sense to me. Is
>>
>> int x = 42;
>> ++x = 97;
>>
>> legal in C++?
>
> No.
>
Sure it is:

class foo {
public:
foo(int x) {
}
foo &operator=(int x){}
};
foo &operator++(foo &x) {
return x;
}

int main() {
#define int foo
int x = 72;
++x = 32;
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/word...

Eric Schmidt

8/13/2008 4:46:00 AM

0

Eric Sosman wrote:
> jackie wrote:
>
>> i know that in c plus plus,++x returns l-value while x++ returns r-
>> value,but what is the situation in c,are both ++x and x++ return r-
>> value? i don't know how C99 defines it,thx.
>
>
> In C (all versions), neither ++x nor x++ is an lvalue.
> I'm surprised to hear that this is different in C++, because
> it makes no sense to me. Is
>
> int x = 42;
> ++x = 97;
>
> legal in C++? If so, what is the value of x afterwards?
> (Actually, you can disregard the second question: If the
> answer to the first is anything other than "No," I don't
> want to know anything more about C++.)
>

It yields undefined behavior in C++ because x is modified twice without
an intervening sequence point. But something like &(++x) is legal
(though it seems useless to me).
** Posted from http://www.te... **

Keith Thompson

8/13/2008 7:29:00 AM

0

CBFalconer <cbfalconer@yahoo.com> writes:
> jackie wrote:
>>
>> i know that in c plus plus,++x returns l-value while x++ returns
>> r- value,but what is the situation in c,are both ++x and x++
>> return r- value? i don't know how C99 defines it,thx.
>
> You're wrong.

Just what is it that you think he's wrong about?

(I'm assuming jackie is a "he"; apologies if I'm mistaken.)

In C++, the result of ``++x'' is an lvalue, and the result of ``x++''
is not an lvalue, so he's right about that; it's off-topic, but a
perfectly reasonable introduction to his question.

The rest of the post was a question, which can hardly be wrong, and a
statement that he doesn't know something, which you can hardly assume
is wrong.

> Illustrative snippet:

Illustrative of what?

> volatile int x = 123;
> int y;
>
> y = x;
> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
> else puts("Bad system");
>
> y = x;
> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
> else puts("Bad system");

What is the point of declaring x volatile?

> The operator has nothing to to with lvalues and rvalues. Same in
> C++. Note that in the snippet the first phrase in the test (before
> the &&) is completed before the 2nd phase is begun.

Certainly it does.

In both C and C++, the operand of a prefix or postfix "++" must be a
modifiable lvalue. In C, the result of either operator is not an
lvalue; in C++, one is an lvalue and one isn't.

In any case, your code snippet is equally valid in either C or C++,
and would behave in exactly the same way even if both prefix and
postfix "++" yielded lvalues; you never use them in a context that
requires an lvalue.

So what was your point again?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer

8/13/2008 8:09:00 AM

0

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>
.... snip ...
>
>> Illustrative snippet:
>
> Illustrative of what?

Of the behaviour of x++ and ++x.
>
>> volatile int x = 123;
>> int y;
>>
>> y = x;
>> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
>> else puts("Bad system");
>>
>> y = x;
>> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
>> else puts("Bad system");
>
> What is the point of declaring x volatile?

That ensures that x is accessed for each equality test. That way
it can't be carried along in a working register.

>
>> The operator has nothing to do with lvalues and rvalues. Same in
>> C++. Note that in the snippet the first phrase in the test (before
>> the &&) is completed before the 2nd phase is begun.
>
> Certainly it does.
>
> In both C and C++, the operand of a prefix or postfix "++" must be a
> modifiable lvalue. In C, the result of either operator is not an
> lvalue; in C++, one is an lvalue and one isn't.
>
> In any case, your code snippet is equally valid in either C or C++,
> and would behave in exactly the same way even if both prefix and
> postfix "++" yielded lvalues; you never use them in a context that
> requires an lvalue.
>
> So what was your point again?

To demonstrate the actions of x++ and ++x, and their difference.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.


jackie

8/13/2008 8:30:00 AM

0

On Aug 13, 3:29 pm, Keith Thompson <ks...@mib.org> wrote:
> CBFalconer <cbfalco...@yahoo.com> writes:
> > jackie wrote:
>
> >> i know that in c plus plus,++x returns l-value while x++ returns
> >> r- value,but what is the situation in c,are both ++x and x++
> >> return r- value? i don't know how C99 defines it,thx.
>
> > You're wrong.
>
> Just what is it that you think he's wrong about?
>
> (I'm assuming jackie is a "he"; apologies if I'm mistaken.)
>
> In C++, the result of ``++x'' is an lvalue, and the result of ``x++''
> is not an lvalue, so he's right about that; it's off-topic, but a
> perfectly reasonable introduction to his question.
>
> The rest of the post was a question, which can hardly be wrong, and a
> statement that he doesn't know something, which you can hardly assume
> is wrong.
>
> >                Illustrative snippet:
>
> Illustrative of what?
>
> >     volatile int x = 123;
> >     int          y;
>
> >     y = x;
> >     if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
> >     else                              puts("Bad system");
>
> >     y = x;
> >     if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
> >     else                                    puts("Bad system");
>
> What is the point of declaring x volatile?
>
> > The operator has nothing to to with lvalues and rvalues.  Same in
> > C++.  Note that in the snippet the first phrase in the test (before
> > the &&) is completed before the 2nd phase is begun.
>
> Certainly it does.
>
> In both C and C++, the operand of a prefix or postfix "++" must be a
> modifiable lvalue.  In C, the result of either operator is not an
> lvalue; in C++, one is an lvalue and one isn't.
>
> In any case, your code snippet is equally valid in either C or C++,
> and would behave in exactly the same way even if both prefix and
> postfix "++" yielded lvalues; you never use them in a context that
> requires an lvalue.
>
> So what was your point again?
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org  <http://www.ghoti.ne...
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>     -- Antony Jay and Jonathan Lynn, "Yes Minister"



u're right,i'm a "he". ^-^
and i think u have already give me ur answer,that's both ++x and x++
return r-value in c,do u mean that? thx for ur help

James Kuyper

8/13/2008 12:29:00 PM

0

CBFalconer wrote:
> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>
> ... snip ...
>>> Illustrative snippet:
>> Illustrative of what?
>
> Of the behaviour of x++ and ++x.
>>> volatile int x = 123;
>>> int y;
>>>
>>> y = x;
>>> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
>>> else puts("Bad system");
>>>
>>> y = x;
>>> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
>>> else puts("Bad system");
....
>> In any case, your code snippet is equally valid in either C or C++,
>> and would behave in exactly the same way even if both prefix and
>> postfix "++" yielded lvalues; you never use them in a context that
>> requires an lvalue.
>>
>> So what was your point again?
>
> To demonstrate the actions of x++ and ++x, and their difference.

The question was about the lvalue-ness of x++ and ++x; what does your
example have to do with that question?