By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Lvalue

P: n/a
#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

Nov 14 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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.
Nov 14 '06 #2

P: n/a
raghu wrote:
#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?

--
Ian Collins.
Nov 14 '06 #3

P: n/a
Ian Collins said:
raghu wrote:
>#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?
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.
Nov 14 '06 #4

P: n/a
Richard Heathfield wrote:
Ian Collins said:

>>raghu wrote:
>>>#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?

Why the +1?
I assumed he wanted to increment the sum of a and b.

--
Ian Collins.
Nov 14 '06 #5

P: n/a
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:

>>>raghu wrote:

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

Why the +1?
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.
Nov 14 '06 #6

P: n/a
Richard Heathfield wrote:
Ian Collins said:

>>Richard Heathfield wrote:
>>>Ian Collins said:

raghu wrote:
>#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?

Why the +1?

I assumed he wanted to increment the sum of a and b.


How would that affect the value of c?
I assumed he wanted to increment the sum of a and b and assign it to c.

--
Ian Collins.
Nov 14 '06 #7

P: n/a
Ian Collins said:
Richard Heathfield wrote:
>>
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.
Nov 14 '06 #8

P: n/a
Richard Heathfield wrote:
Ian Collins said:

>>Richard Heathfield wrote:

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) ?
Why?

--
Ian Collins.
Nov 14 '06 #9

P: n/a

Ian Collins wrote:
Richard Heathfield wrote:
Ian Collins said:

>Richard Heathfield wrote:

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

Nov 14 '06 #10

P: n/a
Ian Collins said:
Richard Heathfield wrote:
>Ian Collins said:

>>>Richard Heathfield wrote:

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) ?
Why?
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.
Nov 14 '06 #11

P: n/a
Richard Heathfield wrote:
Ian Collins said:

>>Richard Heathfield wrote:
>>>Ian Collins said:

Richard Heathfield wrote:

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

Why?


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.

--
Ian Collins.
Nov 14 '06 #12

P: n/a
Ian Collins wrote:
raghu wrote:
>#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?
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.
Nov 14 '06 #13

P: n/a
raghu wrote:
#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[i]` 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.

--
Chris "hantwig efferko VOOM!" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Nov 14 '06 #14

P: n/a
ma**********@pobox.com wrote:
Ian Collins wrote:
Richard Heathfield wrote:
Ian Collins said:
>
>>Richard Heathfield wrote:
>>
>>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) ?
>
Why?

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
Nov 14 '06 #15

P: n/a
Martin Ambuhl <ma*****@earthlink.netwrites:
Ian Collins wrote:
>raghu wrote:
>>#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?

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '06 #16

P: n/a
On Tue, 2006-14-11 at 07:59 +0000, Richard Heathfield wrote:
Ian Collins said:
Richard Heathfield wrote:
Ian Collins said:
raghu wrote:

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

Why the +1?
I assumed he wanted to increment the sum of a and b.

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.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use [first_name].[last]@gmail.com
"You're only smart on the outside." -anon.

Nov 14 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.