-1

Why is the output of the following program showing compile time error?
Please explain "lvalue required"

#include<stdio.h>
int main()
{ 
    int a=5;
    printf("%d", ++a++);
    return 0;
}
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    It's not the output of the program, it's an error message from the compiler while attempting (and failing) to compile your program. – Seg Fault Sep 02 '22 at 18:27
  • The short version is, the language is protecting you from shooting yourself in the foot. The long version is a tad more complicated, and are you in C or C++? – Seg Fault Sep 02 '22 at 18:34
  • 1
    The `++` operator increments a variable, storing the incremented value back into the same variable, and returns a value. So the first `++` increments your variable `a`, and returns a value. But then the second `++` tries to increment that value, but it can't, because there's no variable to store the incremented value back into. It's sort of like saying `5++` — totally meaningless. – Steve Summit Sep 02 '22 at 18:45
  • 1
    @3636 Rewa Kher Just write a proposal to the C Standard Committee to make such construction ++a++ valid.:) – Vlad from Moscow Sep 02 '22 at 18:50
  • 1
    In case you are wondering what an "lvalue" is: https://en.cppreference.com/w/c/language/value_category – Bob__ Sep 02 '22 at 18:51
  • 2
    @SteveSummit: That is not a useful rule for students at this level. Source code is just a string of text, and a string of text has no inherent meaning. Humans give it meaning by relating it to things and specifying rules. The rules could just as easily say that `++a` or `a++` produces an lvalue and that `++a++` does have a meaning. OP would need to know what the rules about meaning are before they could apply a rule not to write meaningless code, and that is essentially what their question is. – Eric Postpischil Sep 02 '22 at 18:51
  • "lvalue required" basically means "I need a variable to store something into". You'll probably get the same error if you write things like `5 = a`, or `5 += 1`, or `5++`, or `++5`. These all fail because `5` is a simple value, not a variable you can store values in. – Steve Summit Sep 02 '22 at 18:51
  • @EricPostpischil I want to stand by that argument, because I *do* think that `++a++` is self-evident nonsense, I really do, but the fact that C++ defines it rather demolishes my argument. :-( Comment deleted. – Steve Summit Sep 03 '22 at 11:43

1 Answers1

4

A language-lawyer proof explanation is a bit lengthy, here's an attempt at a simplified explanation:

The "l" in "lvalue" comes from "left" as in a value that can appear on the left hand side of an assignment operation, which includes many named variables:

int a;
a = 42;  // a is an lvalue and can be assigned to

Now due to operator precedence the expression ++a++ is parsed as ++(a++). And since a++ modifies a "later" but, as expression, evaluates to the current value of a it "returns" a copy of this current value of a, a temporary value.

This temporary value is unnamed (it is not a) and it's not an lvalue, wherefore it can't be assigned to.

You can't write a++ = 42 because you'd be assigning to the temporary value, rather than a variable, and for the same reason you can't write ++a++.

Again, you'll have to dive much deeper, and also give yourself some time to develop an intuitive feeling for what an lvalue is, then this will become much clearer.

Seg Fault
  • 1,331
  • 8
  • 16