18
$\begingroup$

In a lot of languages (eg Java, C++, etc), you can use ++ and -- as increment/decrement operators.

What was the origin of ++ and --, and why did the creator of the language include it?


I read over Why add an increment/decrement operator when compound assignments exist?, and while it gives reasons why someone would include those operators into a language, it doesn't say what language did this first, or why the first person to do so made that choice (though the reason is probabably amongst those answers).

$\endgroup$

2 Answers 2

29
$\begingroup$

According to The Development of the C Language by Dennis Ritchie:

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.

$\endgroup$
1
  • 1
    $\begingroup$ Probably implicit is the small size and simplicity of the first compilers. A modern optimizing compiler doesn't care whether you say i = i+1, i+=1 or i++ or ++i in the for loop, it all ends up as the same instructions (possibly without any i, or without any loop, for that matter, or without any instruction at all if the result can be computed at compile time). Back then, however, chances were that each of these generated different code. $\endgroup$ Commented Sep 15 at 3:33
6
$\begingroup$

According to Wikipedia, these operators were introduced by Ken Thompson in the B language. This is the language that directly inspired C. Once this became popular, many other languages simply copied these operators as well.

He may have been inspired to include these operators because CPUs had some built-in auto-increment features. These were commonly used with index registers or memory locations that contained array indexes, so they would automatically step through the array as you used them. One of the most common uses of ++ is in for loops that iterate through arrays.

for (i = 0; i < size; i++) {
    do something with array[i];
}

which is very similar to the way those index registers are used.

$\endgroup$
2
  • 1
    $\begingroup$ "...because CPUs had some built-in auto-increment features." Greg Hewgill's answer seems to partially refute this, at least with respect to the PDP-11. Do you know if there were other machines with auto-increment instructions on the market at the time that B implemented this feature, and whether any B compiler existed for those systems and made use of them? $\endgroup$ Commented Sep 13 at 14:11
  • 3
    $\begingroup$ The next sentence after the one he quoted describes the PDP-7: "The PDP-7, however, did have a few `auto-increment' memory cells,". The PDP-8 also had this. Whether there were B compilers is not really relevant, these things inspired the notion that auto increment is useful. $\endgroup$ Commented Sep 13 at 15:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.