Why does the indexing start with zero in 'C'?

前端 未结 16 2296
执念已碎
执念已碎 2020-11-22 16:22

Why does the indexing in an array start with zero in C and not with 1?

16条回答
  •  情歌与酒
    2020-11-22 16:54

    This question was posted over a year ago, but here goes...


    About the above reasons

    While Dijkstra's article (previously referenced in a now-deleted answer) makes sense from a mathematical perspective, it isn't as relevant when it comes to programming.

    The decision taken by the language specification & compiler-designers is based on the decision made by computer system-designers to start count at 0.


    The probable reason

    Quoting from a Plea for Peace by Danny Cohen.

    • IEEE Link
    • IEN-137

    For any base b, the first b^N non-negative integers are represented by exactly N digits (including leading zeros) only if numbering starts at 0.

    This can be tested quite easily. In base-2, take 2^3 = 8 The 8th number is:

    • 8 (binary: 1000) if we start count at 1
    • 7 (binary: 111) if we start count at 0

    111 can be represented using 3 bits, while 1000 will require an extra bit (4 bits).


    Why is this relevant

    Computer memory addresses have 2^N cells addressed by N bits. Now if we start counting at 1, 2^N cells would need N+1 address lines. The extra-bit is needed to access exactly 1 address. (1000 in the above case.). Another way to solve it would be to leave the last address inaccessible, and use N address lines.

    Both are sub-optimal solutions, compared to starting count at 0, which would keep all addresses accessible, using exactly N address lines!


    Conclusion

    The decision to start count at 0, has since permeated all digital systems, including the software running on them, because it makes it simpler for the code to translate to what the underlying system can interpret. If it weren't so, there would be one unnecessary translation operation between the machine and programmer, for every array access. It makes compilation easier.


    Quoting from the paper:

    enter image description here

提交回复
热议问题