Search code examples
csyntaxlvalue

Understanding the increment operator in C


Why is the following illegal in C?

y = (w + x)++;

According to my book, this is illegal, but I don't see why.


Solution

  • In i++, the value of i is changed. After execution, i's value is one plus its previous value. You can't store a value in w+x, though, and so you can't do any of the following, which all (if they worked) would have more or less the same effect:

    w+x = w+x+1;
    w+x += 1;
    (w+x)++;
    

    Something that can be placed on the left hand side of an assignment is typically called an lvalue (l is for left). The concise way of saying this is that ++ can only be applied to lvalues, and w+x isn't an lvalue. You can read more about lvalues (and other kinds of values) in this question and its answers: