Reputation: 372814
This may seem like a silly question, but why is it that in many languages there exists a prefix and postfix version of the ++
and --
operator, but no similar prefix/postfix versions of other operators like +=
or -=
? For example, it seems like if I can write this code:
myArray[x++] = 137; // Write 137 to array index at x, then increment x
I should be able to write something like
myArray[5 =+ x] = 137; // Write 137 to array index at x, then add five to x
Of course, such an operator does not exist. Is there a reason for this? It seems like a weird asymmetry in C/C++/Java.
Upvotes: 3
Views: 871
Reputation: 434685
Java and C++ have pre- and post- increment and decrement operators because C has them. C has them because C was written, mostly, for the PDP-11 and the PDP-11 had INC
and DEC
instructions.
Back in the day, optimizing compilers didn't exist so if you wanted to use a single cycle increment operator, either you wrote assembler for it or your language needed an explicit operator for it; C, being a portable assembling language, has explicit increment and decrement operators. Also, the performance difference between ++i
and i++
rarely matters now but it did matter in 1972.
Keep in mind that C is almost 40 years old.
Upvotes: 1
Reputation: 340218
I'd guess there are several reasons, I think among the more heavily weighted might be:
Then again, while the idea for pre/post/increment/decrement operators might have been influenced by machine operations, it looks like they weren't put into the language specifically to take advantage of such. Here's what Dennis Ritchie has to say about them:
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few `auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.
Upvotes: 6
Reputation: 210515
Because the -- and ++ operators map to inc
(rement) and dec
(rement) instructions (in addition to adding and subtracting) in the CPU, and these operators are supposed to map to the instructions, hence why they exist as separate operators.
Upvotes: 1
Reputation: 68006
I'll make an assumption. There're lots of use-cases for ++i
/i++
and in many the specific type of increment (pre/post) makes difference. I can't tell how many times I've seen code like while (buf[i++]) {...}
. On the other hand, +=
is used much less frequently, as it rarely makes sense to shift pointer by 5 elements at once.
So, there's just no common enough application where difference between postfix and prefix version of +=
would be important.
Upvotes: 2
Reputation: 215277
As long as y
has no side effects:
#define POSTADD(x,y) (((x)+=(y))-(y))
Upvotes: 4
Reputation: 2771
If I had to guess, it's common to equate:
x += 5;
...with:
x = x + 5;
And for obvious reasons, it would be nonsensical to write:
x + 5 = x;
I'm not sure how else you would mimic the behavior of 5 =+ x
using just the +
operator. By the way, hi htiek!
Upvotes: 0
Reputation: 4206
I guess it's because it's way too cryptic. Some argue that even ++/-- should be avoided, because they cause confusion and are responsible for most buffer overrun bugs.
Upvotes: 1