编程语言为什么从零开始计数_为什么计算机从零开始计数?

编程语言从零开始计数是因为简化了数组中每个元素的内存地址计算,避免了不必要的减法操作。此外,Dijkstra的论文指出,从零开始能更好地表示范围。这种做法源于早期计算机科学的实践,至今仍广泛沿用。
摘要由CSDN通过智能技术生成
编程语言为什么从零开始计数

编程语言为什么从零开始计数

Counting from zero is a very common practice in many computer languages, but why? Read on as we explore the phenomenon and why it is so widespread.

从零开始计数是许多计算机语言中非常普遍的做法,但是为什么呢? 在我们探索该现象以及其如此广泛的原因时,请继续阅读。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader DragonLord is curious about why most operating systems and programming languages count from zero. He writes:

超级用户阅读器DragonLord好奇为什么大多数操作系统和编程语言都从零开始计数。 他写:

Computers traditionally tally numerical values starting from zero. For example, arrays in C-based programming languages start from index zero.

传统上,计算机计算从零开始的数值。 例如,基于C的编程语言中的数组从索引零开始。

What historical reasons exist for this, and what practical advantages does counting from zero have over counting from one?

这有什么历史原因,从零开始计数比从一开始计数有什么实际优势?

Why indeed? As widespread as the practice is, surely there are practical reasons for its implementation.

为什么会这样呢? 尽管这种做法很普遍,但肯定有实践的理由。

答案 (The Answer)

SuperUser contributor Matteo offers the following insights:

超级用户贡献者Matteo提供以下见解:

Counting arrays from 0 simplifies the computation of the memory address of each element.

从0开始对数组进行计数简化了每个元素的内存地址的计算。

If an array is stored at a given position in memory (it’s called the address) the position of each element can be computed as

如果将数组存储在内存中的给定位置(称为地址),则每个元素的位置可以计算为

element(n) = address + n * size_of_the_element
element(n) = address + n * size_of_the_element

If you consider the first element the first, the computation becomes

如果您将第一个元素视为第一个元素,则计算将变为

element(n) = address + (n-1) * size_of_the_element

Not a huge difference but it adds an unnecessary subtraction for each access.

差别不大,但是每次访问都会添加不必要的减法。

Edited to add:

编辑添加:

  • The usage of the array index as an offset is not a requirement but just an habit. The offset of the first element could be hidden by the system and taken into consideration when allocating and referencing element.

    将数组索引用作偏移量不是必需的,而只是一种习惯。 系统可以隐藏第一个元素的偏移量,并在分配和引用元素时将其考虑在内。
  • Dijkstra published a paper “Why numbering should start at zero” (pdf) where he explains why starting with 0 is a better choice. Starting at zero allows a better representation of ranges.

    Dijkstra发表了一篇论文“为什么编号应该从零开始”( pdf ),他解释了为什么从0开始是更好的选择。 从零开始可以更好地表示范围。

If you’re looking to delve deeper into the answer, the Dijkstra paper is an informative read.

如果您想更深入地研究答案,Dijkstra的文章将为您提供丰富的参考资料。



Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不对。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/149225/why-do-computers-count-from-zero/

编程语言为什么从零开始计数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值