473,473 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Lvalue

#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
16 1892
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
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
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
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
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
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
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
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

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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
15
by: Michael Baehr | last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a previously accepted behavior (cast-as-lvalue) is now marked as deprecated, and will cease to function in GCC 3.5. This has...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
9
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the...
3
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that...
10
by: the_init | last post by:
Hi friends, I read about Lvalue in previous posting and Googled it but I'm not understood it completely. There is a small doubt. int a; a=20; // here a is Lvalue But
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
6
by: Yarco | last post by:
I've alway thought lvalue means Left Value and rvalue means Right Value before i've read someone's article. It is said "lvalue = location value" and "rvalue = read value". Which one is right, then?
33
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.