Why does the indexing in an array start with zero in C and not with 1?
This question was posted over a year ago, but here goes...
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.
Quoting from a Plea for Peace by Danny Cohen.
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:
111
can be represented using 3
bits, while 1000
will require an extra bit (4 bits).
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!
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: