Why does the C programming language (and other C-like languages) have pre and post operators?

This is an interesting question that has a common answer that turns out to be a myth/historically incorrect.

TLDR; the definitive answer to this question comes from Dennis Ritchie himself, developer of the C language in his article “The Development of the C Language” – http://www.bell-labs.com/usr/dmr/www/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.

From http://www.bell-labs.com/usr/dmr/www/chist.html

The common answer which turns out to be incorrect is (paraphrasing) “C has pre and post operators because C was developed on the PDP-11 and this machine had pre and post CPU instructions, the implementation of ++ and — in C used these machine instructions”.

The explanation from Ritchie that corrects this misconception:

“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”

From: http://www.bell-labs.com/usr/dmr/www/chist.html

An interesting piece of computing history.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.