473,320 Members | 1,876 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.

a basic question,need help

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.
Aug 13 '08 #1
16 1179
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
es*****@ieee-dot-org.invalid
Aug 13 '08 #2
[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" <es*****@ieee-dot-org.invalid>
??????:4s******************************@comcast.co m...
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
es*****@ieee-dot-org.invalid

Aug 13 '08 #3
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.
Aug 13 '08 #4
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.att.net>
Try the download section.

Aug 13 '08 #5
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/wordpress/>
Aug 13 '08 #6
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.teranews.com **
Aug 13 '08 #7
CBFalconer <cb********@yahoo.comwrites:
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.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #8
On Aug 13, 3:29*pm, Keith Thompson <ks...@mib.orgwrote:
CBFalconer <cbfalco...@yahoo.comwrites:
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.net/~kst>
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
Aug 13 '08 #9
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
.... 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.att.net>
Try the download section.
Aug 13 '08 #10
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
... 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?
Aug 13 '08 #11
James Kuyper wrote:
CBFalconer wrote:
>Keith Thompson wrote:
>>CBFalconer <cb********@yahoo.comwrites:
... 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?
Well, for one thing you can see immediately that x++ can't have an
lvalue. There is nothing holding that value, apart from the
reference y.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Aug 13 '08 #12
On Aug 13, 4:25 am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
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 is syntactically legal, but it invokes undefined behaviour same as
it would in C if ++x was an lvalue (the same object is modified twice
without intervening sequence point). Something that is legal and makes
a bit of sense would be:

int x = 42;
int *p = &++x;

This will give you x = 43, and p = &x. There is motivation for this
because of "references" in C++. If you have a C++ function

int f (int& x) { return x++; }

then you can call

int x = 42;
int y = f (++x);

which will increase x by one, pass x "by reference" to the function f
which is rather the same as passing the address; f will increase x in
the caller (because it has a reference to it) and return the value
before the incremenent. Effectively this will set x to 44 and y to 43,
and there is no undefined behavior because there are plenty of
sequence points.
Aug 13 '08 #13
jackie <ja*******@gmail.comwrites:
[...]
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
Yes, both ++x and x++ yield a non-lvalue in C. (You can call that an
"rvalue", but C doesn't use the term except in a single footnote.)

Incidentally, silly abbreviations like "u", "ur", and "thx" are
frowwned upon here. Please take the time to spell out simple words
like "you", "your", and "thanks". Not everyone here speaks English as
a native language -- and those of us who do generally prefer to read
actual English.

Also, when you post a followup, it's rarely necessary to quote the
entire parent article. Delete any quoted material that's not relevant
to your followup, leaving just enough so that your article makes sense
to someone who hasn't seen the parent. In particular, don't quote
signatures unless you're actually commenting on them.

Thx. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #14
CBFalconer wrote:
James Kuyper wrote:
CBFalconer wrote:
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:

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

Well, for one thing you can see immediately that x++ can't have an
lvalue. There is nothing holding that value, apart from the
reference y.
Actually, it is ++x, not x++, which is an lvalue in C++, and your code
does nothing to demonstrate that "There is nothing holding" the value
of ++x, because you code makes no use of ++x in a context that
requires an lvalue.
The example provided by Daniel Pits does use ++x in a context that
requires an lvalue. The fact that his code compiles and works as he
expected it to work demonstrates that jackie was in fact correct in
saying that ++x is an lvalue in C++, your assertions to the contrary
notwithstanding.

Actually, as a demonstration, Daniel's code could be improved upon. It
would be clearer what the lvalued-ness of ++x means in C++, if his foo
class had contained a non-static data member that participated in
operator++() and operator=() in a non-trivial fashion. I assure you
that adding such a data member would not prevent the code from
working. If he had done so, it would have been quite clear that there
is indeed something "holding that value". The "something" is what C++
calls an temporary object.

The fact that C cannot usefully make ++x an lvalue is due the fact
that C structs cannot have member functions. The fact that C has no
need to make ++x an lvalue is mainly due to the fact that C doesn't
have operator overloads. It's not because it would be impossible to
make ++x an lvalue in C; it would have been just as feasible for C to
introduce the concept of a temporary object.

Aug 13 '08 #15
CBFalconer <cb********@yahoo.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
... snip ...
The above "... snip ..." includes you claiming that the original
poster is wrong, and me asking you what you think he's wrong about.

So, just what do you think the original poster was wrong about? As
far as I can tell, he was quite correct.
>>Illustrative snippet:

Illustrative of what?

Of the behaviour of x++ and ++x.
Not of any behavior relevant to the question.
>> 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.
Why would storing the value of x in a working register be a problem?
The behavior of your code snippet is well defined, and exactly the
same, with or without the "volatile" (unless the accesses to x are
intended to have some side effect you're not telling us about, but
that doesn't seem likely).

To put it another way, why not make y volatile as well?
>>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.
But that wasn't what anybody was asking about. The question was
whether ++x or x++ yields an lvalue. That's a perfectly legitimate
question, to which an answer of "No" would have been entirely correct
and sufficient.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 13 '08 #16
On 13 Aug 2008 at 14:55, CBFalconer wrote:
Well, for one thing you can see immediately that x++ can't have an
lvalue. There is nothing holding that value, apart from the
reference y.
My god, you really have gone off into la-la land in your dotage, haven't
you? Your last sentence doesn't make a bit of sense. And y is not a
reference in your code (as you're normally the first to say, C doesn't
have references).

Aug 13 '08 #17

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

Similar topics

8
by: Cherrish Vaidiyan | last post by:
hello googles, I have a small sqlplus problem. i have created a table with date field along with other varchar2,number etc. But unfortunately i made a mistake in entering the date. for some date...
1
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
2
by: Ryan McBride | last post by:
Once again at my wonderful job i've been given the task of "come teach your fellow idiot coworkers the skills you have" I write software for a company in chicago. I use visual basic on asp.net...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
6
by: kathy | last post by:
I have a pointer: MyClass *p = NULL; p = new MyClass(...); .... delete p; After delete p, does p equal NULL(it is in C++ standard?)? How to decide if p has been deleted?
1
by: suresh_punniyakkodi | last post by:
Hellow, I am suresh, i am developing one project in VB. I need your help. My question is how to zoom pictures(zoom in and zoom out) in Visual Basic (like Photo Editor). ...
4
by: corky20 | last post by:
Can someone help...i've only started learning visual basic and i'm pretty new to programming(i know but everyones been there at one point) and need help with a question! A man is paid at the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
14
by: princedhump | last post by:
Hi everyone, I am making a game in Visual Basic. I need some help with it. My question is: that when the user would click the Next button on the Form, the next Question should show! I have 15...
12
by: sheldonlg | last post by:
This is kind of basic, and I googled but didn't find much help. I have an array of text element fields. They all need to have the same name. So, let the name be either all with a or build...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.