0

int a = 10;
int b = ++a++;
printf(b);

The code when compied shows lvalue error.

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by WaltP
0

You're trying to apply the prefix ++ operator to the result of the expression a++. The result of a++ is a temporary value and thus not assignable, i.e. it is not an lvalue. Since you're only allowed to use ++ on lvalues, you get the error that you get.

Somewhat interesting side note: The result of a++ is an rvalue in both C and C++. However the result of ++a is an lvalue in C++, but not in C. So if you changed your code to say (++a)++, your code would work in C++, but not in C.

1

So if you changed your code to say (++a)++, your code would work in C++, but not in C.

No it won't. Pre- and post- incrementing the same value is called undefined behavior, therefore the result may be different on different compilers. The result is ambiguous. See this

0

Let me correct myself: It would compile in C++, but not in C. Though now that I think about it, a compiler might theoretically refuse to compile a program that it know invokes UB, right? So that's not necessarily true either...

1

I guess theoretically. But in practice, it should compile fine.

After a test, I could not get a clean compile at all. But since it's undefined, I really don't care... ;o)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.