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

The code when compied shows lvalue error.

Recommended Answers

All 4 Replies

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.

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

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...

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.