Lvalue

R

raghu

#include<stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
c=(a+b)++;
printf("%d",c);
return 0;
}

For the above program I got the error as Lvalue required.I compiled in
Turbo C/C++ compiler.

Can anyone please explain why the error is and how to eliminate it .

Thanks a lot.

Regards,
Raghu
 
I

Ian Collins

raghu said:
#include<stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
c=(a+b)++;
White space isn't expensive and makes code easier to read.

(a+b) is an expression, not a modifiable value. Why not c = a+b+1?
 
R

Richard Heathfield

raghu said:
#include<stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
c=(a+b)++;
printf("%d",c);
return 0;
}

For the above program I got the error as Lvalue required.

That's right, yes. ++ affects the value of an object. Arbitrary expressions
won't do.
Can anyone please explain why the error is and how to eliminate it .

++ increases the value of an object by 1. (a+b) is not an object. To
eliminate the error, remove the ++:

#include<stdio.h>

int main(void)
{
int a = 1;
int b = 2;
int c = 0;

c = a + b;
printf("%d", c);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
R

Richard Heathfield

Ian Collins said:
White space isn't expensive and makes code easier to read.

(a+b) is an expression, not a modifiable value. Why not c = a+b+1?

Why the +1?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
R

Richard Heathfield

Ian Collins said:
I assumed he wanted to increment the sum of a and b.

How would that affect the value of c?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
R

Richard Heathfield

Ian Collins said:
I assumed he wanted to increment the sum of a and b and assign it to c.

If so, would he not have written ++(a + b) ?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
M

mark_bluemel

Ian said:

I think Richard's point was that the postfix "++" implies adding one
_after_ c had been assigned the value of "a+b"... Quite what the one
would be added to remains a mystery.
 
R

Richard Heathfield

Ian Collins said:

Because x++ is a post-increment for x, which yields x's old value as its
result. If (a+b)++ were meaningful (which, of course, it is not), I would
expect it to yield a+b as its value, not a+b+1.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
I

Ian Collins

Richard said:
Ian Collins said:




Because x++ is a post-increment for x, which yields x's old value as its
result. If (a+b)++ were meaningful (which, of course, it is not), I would
expect it to yield a+b as its value, not a+b+1.
OK, it's been a long day.
 
M

Martin Ambuhl

Ian said:
White space isn't expensive and makes code easier to read.

(a+b) is an expression, not a modifiable value. Why not c = a+b+1?

Had the erroneous line been
c = ++(a+b);
then your suggestion would make sense, since the value of ++x is x+1.
The value of x++ is x, so your '+1' cannot be right.
 
C

Chris Dollin

raghu said:
#include<stdio.h>
int main(void)
{
int a=1;
int b=2;
int c=0;
c=(a+b)++;
printf("%d",c);
return 0;
}

For the above program I got the error as Lvalue required.I compiled in
Turbo C/C++ compiler.

Can anyone please explain why the error is and how to eliminate it .

In the expression `X++`, X must be a variable [1]. `a + b` isn't a
variable. The compiler correctly complains.

To eliminate this, don't do it. Decide what you mean and do that
instead.

[1] Technically, a "modifiable lvalue". Historically, the term
"lvalue" meant "the /value/ you get when you evaluate an
expression on the /l/eft of a [traditional] assignment".
Expressions like `x + y` don't have lvalues in C (they do
in some other languages -- no modern ones that I know of,
though, not that that means much. "Next 700 programming
languages, ha, 700 is a *wimp*.).

C's lvalue means something like "an expression that
could designate an object". Which means a non-const
variable or `*E` (and hence `A` for fitting `A`
and `i`) where E's type is pointer-to-nonconst-object-
type-and-that-excludes-void-and-it-mustnot-be-null-either.
I'm sure there's something I've forgotten but that should
get you started.
 
R

Richard Bos

I think Richard's point was that the postfix "++" implies adding one
_after_ c had been assigned the value of "a+b"...

It does, but given the line that he did use, I see no reason to assume
that much knowledge about the ++ operator(s) in the OP.

Richard
 
K

Keith Thompson

Martin Ambuhl said:
Had the erroneous line been
c = ++(a+b);
then your suggestion would make sense, since the value of ++x is x+1.
The value of x++ is x, so your '+1' cannot be right.

The OP wrote a statement, "c=(a+b)++;", that's illegal; to someone who
understands what the "++" operator does, the statement doesn't make
any sense, any more than "5++" or "(a+b) = 42;".

So the question is, what was the OP *trying* to do. I've found that
some newbies misunderstand the "++" operator, thinking that "x++" is a
convenient way of writing "x+1". Someone with that particular
misconception isn't going to think about the distinction between
prefix and postfix "++".

So yes, it's reasonable to guess that "c=(a+b)++;" is a mistaken way
of writing "c=a+b+1".
 
A

Andrew Poelstra

Ian Collins said:


How would that affect the value of c?

The same way it might do anything else: this guy wasn't using C,
and therefore no one can tell what he meant by this C-looking code.

You assumed (a+b).
Ian assumed (a+b)+1.
I assumed a + b++.

So, it's an interesting example of a "what on earth does that mean?"
maintenance scenario, and shows why to write proper, concise code.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top