I'm reading "The C programming language" by Brian W. Kernighan and Dennis M. Ritchie, on page 46 its states that "The increment and decrement operators can only be applied to variables; an expression like (i+j)++
is illegal". Why can't it be used else where only before or after variables?

- 8,380
- 3
- 30
- 45
-
8What would you expect `(i+j)++` to do? Is there another example that you would expect to work? – Potatoswatter Aug 07 '15 at 05:37
-
Ask yourself: what exactly would you want the illegal expression to mean? What would be incremented or decremented and why would that be useful? – Keith Aug 07 '15 at 05:37
-
1Because part of the definition of those operators is "update the variable they are applied to" . It's not the same as `+ 1` . – M.M Aug 07 '15 at 05:37
-
Because it would look pretty silly in the middle of a variable. `fo++o`? I don't even... – Ignacio Vazquez-Abrams Aug 07 '15 at 06:35
-
@IgnacioVazquez-Abrams haha I didnt mean to put the actual ++ or -- in the middle of the variables name, but why cant we use the operators on other things not just variables. – Arlind Hajredinaj Aug 07 '15 at 08:52
3 Answers
I'm not sure what you mean by "only before variables". Operators ++
and --
(both postfix and prefix) require modifiable lvalues as their operands. Lvalue is not necessarily represented by an immediate variable name.
For example, you can do this
int a[10] = { 0 };
++*(a + 5);
Is *(a + 5)
a "variable" in your understanding?
The problem with i + j
is not that it is "not a variable". The problem with i + j
is that it is not an lvalue. Which is why you can't apply ++
to it.
In C parlance the term "variable" is sometimes used as a semi-informal synonym of the term "modifiable [scalar] object", which in turn is synonymous with the term "modifiable lvalue [of scalar type]". The book you were referring to might have use the term "variable" in that semi-informal sense. In that sense *(a + 5)
is also a "variable".

- 312,472
- 42
- 525
- 765
-
What I mean is you can only apply ++ or - - before or after variables as in a++ or ++a. You are correct, its just that we already know that ++ -- can only be applied to variables, the question is why is this so. btw great explanation :) – Arlind Hajredinaj Aug 07 '15 at 05:46
-
So I was reading on lvalue and rvalue, so what ++ and -- are only used to increment and decrement values in memory, and cannot do the same with "right" values rvalues. – Arlind Hajredinaj Aug 07 '15 at 05:51
-
@Arlind: Well, yes, "lvalue" means "value in memory". But what you mean by "raw values rvalues" is not entirely clear. – AnT stands with Russia Aug 07 '15 at 05:52
can you do 8++?
The operand must have an arithmetic or pointer data type, and must refer to a modifiable data object.

- 1,635
- 1
- 11
- 24
Because (i+j) is a result of adding two variables, you have no variable that actually stores i+j it's just a computed result so it's like saying, let's say i = 1, and j = 2, i+j is equal to 3, and 3++ is not valid because 3 is an r-value. For more information go here, http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c

- 1,892
- 16
- 31