Search code examples
cincrementlvaluedecrementoperand

why lvalue required as increment operand error?


Why lvalue required as increment operand Error In a=b+(++c++); ?

Just Wanted to assign 'b+(c+1)' to 'a' and Increment 'C' by 2 at the same time.

I'M A Beginner Just Wanted A Clarification About What "LVALUE ERROR" Actually Is?

main()
{

int a=1,b=5,c=3;

a=b+(++c++);  

printf("a=%d   b= %d   c= %d \n",a,b,c);
}

Solution

  • Postfix increment binds tighter than prefix increment so what you would want would be something like:

    a = b + (++c)++;
    

    This is not legal C, though, as the the result of prefix increment (like the result of postfix increment in your example) is not an lvalue. This means that it's just a value; it doesn't refer to a particular object like 'c' any more so trying to change it makes no sense. It would have no visible effect as no object would be updated.

    Personally I think that doing it in two statements is clearer in any case.

    a = b + c + 1;
    c += 2;