-3

I just wrote a code in c

#include <stdio.h>

int main()
{
    int a=0;
    option1: a++=5;
    option2: ++a=5;
    printf("%d\n",a);

    return 0;
}

but it doesnt compiled with error

lvalue required as left operand of assignment

why its an error? thanks!

Dvir Naim
  • 335
  • 1
  • 3
  • 12
  • 1
    `option1: a++=5;` is not a C code. – Vagish Feb 17 '15 at 12:44
  • 5
    @Vagish Actually it is. Goto labels! – HolyBlackCat Feb 17 '15 at 12:50
  • @HolyBlackCat: he is trying to achieve something like `a=6=5`.This is not valid. – Vagish Feb 17 '15 at 12:51
  • Oh now I see,sorry for the confusion.Comment was not about label. – Vagish Feb 17 '15 at 12:54
  • Thank you all, I'm pretty new in C and I tried my best to search but it's a problem to write "a++=" in google.. the downvote is It is quite insulting.... – Dvir Naim Feb 17 '15 at 12:57
  • 1
    You might have been downvoted because neither "option" actually makes sense (in other words: we don't even know what you are trying to do). It is often a good idea to explain what you are attempting to do. – crashmstr Feb 17 '15 at 13:12
  • 1
    Maybe it was downvoted for the lack of research, a search for the error message gives a bunch of results, and (after some research about what an lvalue is) the error message is quite self-explanatory. – mafso Feb 17 '15 at 13:33

5 Answers5

5

Because, like your compiler says, a++ is not an lvalue, it's a rvalue. You will find more information about lvalue and rvalue here.

nouney
  • 4,363
  • 19
  • 31
4

A variable is a name associated to a storage. When you define a variable you associates a symbol to a reserved storage. In a program you can use the name of a variable at different places. In an arithmetic expression, like a+1, a refers to the value stored in the memory associated to the symbol. When you use it in an assignment like a=3, a refers to the storage location.

When the symbol is used to denote the value, it is said to be an r-value (right value, value at the right of an assignment), when the symbol is used to denote the location, it is said to be an l-value (left value, value at the left of an assignment). In a=b, a is a l-value and b a r-value.

You can only have l-values at the left of an assignment, alas a++ is an expression that denotes the value of the variable before the increment, not the location, so you can't put it on the left of an assignment.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

well a++ simply is a shortcut to write a=a+1 nothing more than that. there is a difference in a++ and ++a but both ultimately add 1 to initial value of the variable (here that variable is a).a++=5 is not a valid syntax. example: suppose we have a initial value a=3, now if you write a++ == 5(note == which means equals to in coding) it means 4==5 which is not true. but since you're using "=" your code doen't make any sense to the compiler.

Fclass
  • 69
  • 6
0

It is not a valid syntax.

a++; //it will give 1. so after that you are assinging

So it is like

1=5;

It will give you the error. Refer this link.

Community
  • 1
  • 1
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
-1

a++=5 is the same as (a++)=5

And (a++) will evaluate to 1, and you can't do 1=5

MTilsted
  • 5,425
  • 9
  • 44
  • 76