Why does the indexing start with zero in 'C'?
Why does the indexing in an array start with zero in C and not with 1?
Solution 1:
In C, the name of an array is essentially a pointer [but see the comments], a reference to a memory location, and so the expression array[n]
refers to a memory location n
elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]
.
For more info:
http://developeronline.blogspot.com/2008/04/why-array-index-should-start-from-0.html
Solution 2:
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:
Solution 3:
Because 0 is how far from the pointer to the head of the array to the array's first element.
Consider:
int foo[5] = {1,2,3,4,5};
To access 0 we do:
foo[0]
But foo decomposes to a pointer, and the above access has analogous pointer arithmetic way of accessing it
*(foo + 0)
These days pointer arithmetic isn't used as frequently. Way back when though, it was a convenient way to take an address and move X "ints" away from that starting point. Of course if you wanted to just stay where you are, you just add 0!
Solution 4:
Because 0-based index allows...
array[index]
...to be implemented as...
*(array + index)
If index were 1-based, compiler would need to generate: *(array + index - 1)
, and this "-1" would hurt the performance.
Solution 5:
Because it made the compiler and linker simpler (easier to write).
Reference:
"...Referencing memory by an address and an offset is represented directly in hardware on virtually all computer architectures, so this design detail in C makes compilation easier"
and
"...this makes for a simpler implementation..."