Something that puzzles almost every programming beginner is this question: "Why is the first element number 0 instead of number 1?" It feels out of step with everyday intuition, but in the world of computers, starting at 0 has powerful advantages, both mathematically and structurally.
Let's unpack the reason from two angles: "memory addresses" and "the elegance of calculation."
1. An index is a "distance (offset)"
This is the most fundamental reason. An array is data laid out in a "contiguous row of boxes" in memory. When a computer looks up the location of a particular element, it does the following calculation behind the scenes.
element address = start address + (index × element size)
If arrays started at 1, the calculation would look like this every time.
element address = start address + ((index - 1) × element size)
You'd have to subtract 1 every single time. That is wasted work for the computer, whereas starting at 0 needs only a very simple addition: "start address + distance (index)". In other words, index 0 means "distance 0 from the start."
2. Ranges become cleaner
For a 0-based array of lengthn, expressing the range works out very neatly in mathematical terms.
- Index range:
0 <= i < n - Slice ranges and the like:
[start, end)(start is included, end is not)
1 <= i <= n, and the element count is off unless you add + 1 after the subtraction. When writing algorithms, the presence or absence of that "plus 1" is a common source of bugs (off-by-one errors), so 0-based indexing is safer.
3. Historical inheritance (the influence of C)
Many modern languages (Java, Python, JavaScript, and others) are 0-based because they strongly inherit the design of C.
In C, the array subscripta[i] is merely a rewriting (syntactic sugar) of the pointer arithmetic *(a + i). Since adding to a pointer meant "an offset from the start," 0-based indexing was inevitable.
This design was so rational that later languages following it became the de facto standard.
Summary
Counting from 1 is natural for humans, but computing an offset from 0 is natural for computers.
- 0 means "distance from the start"
- Subtraction in range calculations works out cleanly
- A rational design inherited from C
array[0], picture it as the spot at "distance 0" that points directly at the starting address in memory.