Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the historical reasons C languages have pre-increments and post-increments?

(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.)

Developers of C (Dennis Ritchie et al) created increment and decrement operators for very good reasons. What I don't understand is why they decided to create the distinction of pre- vs post- increments/decrements?

My sense is that these operators were far more useful when C was being developed than today. Most C/C++ programmers use one or the other, and programmers from other languages find the distinction today bizarre and confusing (NB: this is based solely on anecdotal evidence).

Why did they decide to do this, and what has changed in computation that this distinction isn't so useful today?

For the record, the difference between the two can be seen in C++ code:

int x = 3;

cout << "x = 3; x++ == " << x++ << endl;
cout << "++x == " << ++x << endl;
cout << "x-- == " << x-- << endl;
cout << "--x == " << --x << endl;

will give as an output

x++ == 3
++x == 5
x-- == 5
--x == 3
like image 726
ShanZhengYang Avatar asked May 25 '15 00:05

ShanZhengYang


People also ask

What are pre-increment and post increment operators in C explain how they work differently?

Preincrement and Postincrement in C are the two ways to use the increment operator. In Pre-Increment, the operator sign (++) comes before the variable. It increments the value of a variable before assigning it to another variable. In Post-Increment, the operator sign (++) comes after the variable.

Which one is more efficient and why pre-increment or post increment?

As a result, pre-increment is faster than post-increment because post-increment keeps a copy of the previous value where pre-increment directly adds 1 without copying the previous value.

What is the use of pre-increment?

1) Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in an expression. In the Pre-Increment, value is first incremented and then used inside the expression.

What is pre-increment and pre decrement in C?

Increment operators are used to increase the value by one while decrement works opposite increment. Decrement operator decreases the value by one.


3 Answers

Incrementing and decrementing by 1 were widely supported in hardware at the time: a single opcode, and fast. This because "incrementing by 1" and "decrementing by 1" were a very common operation in code (true to this day).

The post- and predecrement forms only affected the place where this opcode got inserted in the generated machine code. Conceptually, this mimics "increase/decrease before or after using the result". In a single statement

i++;

the 'before/after' concept is not used (and so it does the same as ++i;), but in

printf ("%d", ++i);

it is. That distinction is as important nowadays as it was when the language C was designed (this particular idiom was copied from its precursor named "B").

From The Development of the C Language

This feature [PDP-7's "`auto-increment' memory cells"] probably suggested such operators to Thompson [Ken Thompson, who designed "B", the precursor of C]; 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.

Thanks to @dyp for mentioning this document.

like image 121
Jongware Avatar answered Oct 10 '22 13:10

Jongware


When you count down from n it is very important whether is pre-decrement or post-decrement

#include <stdio.h>
void foopre(int n) {
    printf("pre");
    while (--n) printf(" %d", n);
    puts("");
}
void foopost(int n) {
    printf("post");
    while (n--) printf(" %d", n);
    puts("");
}
int main(void) {
    foopre(5);
    foopost(5);
    return 0;
}

See the code running at ideone.

like image 44
pmg Avatar answered Oct 10 '22 11:10

pmg


To get an answer that goes beyond speculation, most probably you have to ask Dennis Ritchie et al personally.

Adding to the answer already given, I'd like to add two possible reasons I came up with:

  • lazyness / conserving space:

    you might be able to save a few keystrokes / bytes in the input file using the appropriate version in constructs like while(--i) vs. while(i--). (take a look at pmg s answer to see, why both make a difference, if you didn't see it in the first run)

  • esthetics

    For reasons of symmetry having just one version either pre- or postincrement / decrement might feel like missing something.

EDIT: added sparing a few bytes in the input file in the speculation section providing, now providing a pretty nice "historic" reason as well.

Anyways the main point in putting together the list was giving examples of possible explanations not being too historic, but still holding today.

Of course I am not sure, but I think asking for a "historic" reason other than personal taste is starting from a presumtion not neccesarily true.

like image 2
mikyra Avatar answered Oct 10 '22 13:10

mikyra