C语言先增加后增加的历史原因是什么? [英] What are the historical reasons C languages have pre-increments and post-increments?

查看:119
本文介绍了C语言先增加后增加的历史原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:我不是在问前置增量与后置增量的定义,或者它们在C/C ++中的用法.因此,我认为这不是重复的问题.)

(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.)

C的开发人员(Dennis Ritchie等人)出于很好的理由创建了增量和减量运算符.我不明白的是,他们为什么决定区分增量前/后增量/减量?

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?

我的感觉是,当开发C时,这些运算符比今天有用得多.大多数C/C ++程序员使用一种或另一种,其他语言的程序员今天发现这种区别离奇而混乱(注意:这完全是基于轶事证据).

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?

为便于记录,两者之间的区别可以在C ++代码中看到:

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;

将给出输出

x++ == 3
++x == 5
x-- == 5
--x == 3

推荐答案

当时在硬件中广泛支持递增和递减1:单个操作码和 fast .这是因为递增1"和递减1"是代码中很常见的操作(直到今天仍然如此).

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++;

未使用之前/之后"概念(因此与++i;相同),但在

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

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

是.如今,这种区别与设计C语言时一样重要(该特殊习惯用法是从其前身"B"复制而来的.)

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").

来自 C语言的发展

此功能[PDP-7的自动递增"存储单元"]可能向Thompson [肯·汤普森(Ken Thompson)设计了C的前身"B")提出了这种建议.使它们都带有前缀和后缀的概括是他自己的.的确,自动递增单元并没有直接用于运算符的实现中,创新的更强动力可能是他观察到++ x的翻译小于x = x + 1的翻译.

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.

感谢@dyp提及此文档.

这篇关于C语言先增加后增加的历史原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆