Lvalue

Discussion in 'C Programming' started by raghu, Nov 14, 2006.

  1. raghu

    raghu Guest

    #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
    raghu, Nov 14, 2006
    #1
    1. Advertising

  2. raghu

    Ian Collins Guest

    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.
    Ian Collins, Nov 14, 2006
    #2
    1. Advertising

  3. 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.
    Richard Heathfield, Nov 14, 2006
    #3
  4. 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.
    Richard Heathfield, Nov 14, 2006
    #4
  5. raghu

    Ian Collins Guest

    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.
    Ian Collins, Nov 14, 2006
    #5
  6. 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.
    Richard Heathfield, Nov 14, 2006
    #6
  7. raghu

    Ian Collins Guest

    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.
    Ian Collins, Nov 14, 2006
    #7
  8. 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.
    Richard Heathfield, Nov 14, 2006
    #8
  9. raghu

    Ian Collins Guest

    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.
    Ian Collins, Nov 14, 2006
    #9
  10. raghu

    Guest

    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, 2006
    #10
  11. 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.
    Richard Heathfield, Nov 14, 2006
    #11
  12. raghu

    Ian Collins Guest

    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.
    Ian Collins, Nov 14, 2006
    #12
  13. 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.
    Martin Ambuhl, Nov 14, 2006
    #13
  14. raghu

    Chris Dollin Guest

    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` 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/
    Chris Dollin, Nov 14, 2006
    #14
  15. raghu

    Richard Bos Guest

    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
    Richard Bos, Nov 14, 2006
    #15
  16. Martin Ambuhl <> writes:
    > 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) <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.
    Keith Thompson, Nov 14, 2006
    #16
  17. 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.
    Andrew Poelstra, Nov 14, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. srinivas reddy

    lvalue with increment operators

    srinivas reddy, Jul 7, 2003, in forum: C++
    Replies:
    1
    Views:
    393
    Victor Bazarov
    Jul 7, 2003
  2. Victor Bazarov

    Re: lvalue with increment operators

    Victor Bazarov, Jul 8, 2003, in forum: C++
    Replies:
    0
    Views:
    407
    Victor Bazarov
    Jul 8, 2003
  3. Kavya
    Replies:
    9
    Views:
    507
    Dik T. Winter
    Oct 28, 2006
  4. Replies:
    11
    Views:
    693
    James Kuyper
    Sep 22, 2008
  5. Julian Mehnle
    Replies:
    0
    Views:
    238
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page