Suppose we have an example of the code shown below in C. Will it compile? How do lvalues and rvalues work in this problem?
#define X 8 int main(void) { ++X;
The concepts of lvalues and rvalues should be explained a bit in order to really understand the above code and the problem statement. Before we continue, you should note that the definition of lvalues and rvalues presented here is not accurate, since even the C standards themselves are rather vague in definition.
Difference between values and lvalues
An object is a region of memory that can be checked, but not necessarily changed. Lvalue is an expression that refers to such an object. The term lvalue originally referred to objects that appear on the left (hence the "l") side of the expression. This definition is no longer applicable, since any const-qualified type is also considered an lvalue, but it can never appear on the left side of the assignment operator, because it cannot be changed. Thus, the term “lvalue modifiable value” was created to mean the lvalue value that can be changed, and the type corresponding to const does not fall into this category.
The value r is any expression that has a value but cannot have a value assigned to it. You can also say that rvalue is any expression that is not an lvalue. An example of rvalue would be a literal constant - something like 8 'or 3.14'. Thus, it is obvious that the value 8 'in the above code is an rvalue.
Using our understanding of lvalues and rvalues to answer a question
Now let's try to solve the problem. Strictly speaking, the operand of the increment operator of a prefix (or postfix) must be a modifiable value of lvalue. So, what is the operand of the prefix increment operator in our code above?
Since X is a macro, the above expression will expand to "++ 8" after starting the preprocessor. This means that "8" is the operand of the prefix increment operator. And since 8 is an rvalue value, it cannot be used as an argument to "++". This, in turn, means that the code above will not compile.