Why do programmers count from zero?

Ask a person for the first item in a list and they will point to item one. Ask a
program for the first item in an array and you may have to write items[0].
It can look as though programmers have forgotten how to count.
They have not. First is an ordinal: it tells you an item’s place in a human sequence. An array index can answer a different question: how far is this item from the beginning? The first item is zero steps away.
That small change of viewpoint explains much of zero-based indexing. It also explains why the convention is useful without pretending that every language must adopt it.
An index as an offset
Imagine three cards in a row: red, green and blue. The red card has no cards
before it, the green card has one, and the blue card has two. Those counts give
them indexes 0, 1 and 2.
In C, this offset interpretation is especially literal. The expression a[i]
means the element reached by moving i elements from the beginning of the
array: it is equivalent to *(a + i). The GNU C manual
describes this relationship directly. At i = 0, nothing is added to the
starting address, so the first element is right there.
This is a useful explanation for C’s indexing model, not a claim that pointer
arithmetic invented the convention or that every language implements arrays
as C does. In JavaScript arrays,
the same visible numbering applies even though programmers do not work with C
pointers: the first element has index 0, and the last has index length - 1.
For an array of three cards, the valid indexes are 0, 1 and 2. Its length
is still 3 because it contains three cards. Index 3 marks the position
after the last card; it is not a fourth card you can read.
Why the end is left out
Zero-based indexes become even more helpful when a range includes its start
but excludes its end. Such a range is written mathematically as [start, end).
The cards at indexes 0, 1 and 2 occupy the range [0, 3). Its size is
simply 3 - 0 = 3.
The same rule works for a smaller piece. In JavaScript, items.slice(1, 3)
returns the elements at indexes 1 and 2, not the element at 3. The
documentation for slice
defines its end as exclusive. The result has 3 - 1 = 2 elements.
Half-open ranges also fit together without an overlap or a gap. The range
[0, 1) contains the first card; [1, 3) contains the next two. Both pieces
meet at boundary 1. Python’s tutorial
uses this boundary model to explain slices: the left edge is included, the
right edge is excluded, and the difference between them gives the length.
This is why a familiar loop often reads i = 0; i < length; i++. It starts at
the first valid offset and stops before the one-past-the-end boundary. The
loop condition and the array’s length describe the same endpoint.
Dijkstra’s argument was not the origin story
In 1982, Edsger Dijkstra wrote a short note called
“Why numbering should start at zero”.
He argued that a number can represent how many elements precede a position.
Starting at zero then makes a sequence of N elements run from 0 up to,
but not including, N.
Dijkstra was defending a convention already in use, not announcing its invention. His note is valuable because it makes the arithmetic behind the choice explicit. A range’s upper bound minus its lower bound tells you how many elements it contains. The zero at the beginning is doing work.
It is a convention, not a law
Other choices work. MATLAB, for example, uses one-based array indexes: its
array-indexing guide
addresses the first row or column with 1. For a person reading a table,
that can feel more natural. Dijkstra’s note itself discusses languages whose
indexing starts at one.
No computer needs to believe that zero is the “real” first number. The best indexing rule depends on the language and the interface. What matters when writing code is knowing which meaning a particular index carries. An ordinal answers which item is this? An offset answers how far from the start?
So programmers do not count three objects as zero, one and two. They count three objects as three. They often label their positions by the distance from the beginning: zero, one and two. Once you see the label as a distance, the first zero stops looking like a mistake.





