comp lang: Should array index start at 0 or 1? (2013)

By Xah Lee. Date: . Last updated: .

There are one bunch of industrial programer idiots, who think that array index should start from 0, for “math” reasons, except these idiots don't know that all math languages start with 1.

Which language's array index starts at 0?

Non-math languages start with 0.

Which language's array index starts at 1?

Math languages start at 1.

Should Array Index Start at 0 or 1?

Counting should start at 1, and ending index should be inclusive. Because that's most intuitive.

This can be easily seen when you have nested list. For example, suppose you have a simple 3D matrix

[ [[7,5],[84,54]], [[1,6],[96,71]], [[9,b],[3,8]] ]

See that b there? what index is it?

In math, vector and matrix's components are 1 based, not 0.

Nested array is heavily used in math, for marix, tensor, tree. This is why most math oriented languages starts at 1.

Python Array Index Idiocy

Python array start at 0, and the ending index does not include the element.

Python's way is the most painful to work with.

# range 1 to 4 gives 1 to 3.
# most annoying
print(list(range(1, 4)))
# [1, 2, 3]

# extracting element also very unintuitive
print(["a", "b", "c"][0:1])
# ['a']

The very fact, that range(1,4) returns [1,2,3] not [1,2,3,4], is source of lots redoes.

Most other langs, even they start at 0, but a ending index usually is inclusive. For example, here's Perl.

# perl

use Data::Dumper;

@xx = qw(a b c);

@yy = @xx[0..1];

print Dumper(\@yy);
# ['a', 'b']

Why Do Programmers Start Counting at Zero

Array Index Mike Hoye m2J4Z
Array Index Mike Hoye m2J4Z

The usual arguments involving pointer arithmetic and incrementing by sizeof(struct) and so forth describe features that are nice enough once you've got the hang of them, but they're also post-facto justifications. This is obvious if you take the most cursory look at the history of programming languages; C inherited its array semantics from B, which inherited them in turn from BCPL, and though BCPL arrays are zero-origin, the language doesn't support pointer arithmetic, much less data structures. On top of that other languages that antedate BCPL and C aren't zero-indexed. Algol 60 uses one-indexed arrays, and arrays in Fortran are arbitrarily indexed – they're just a range from X to Y, and X and Y don't even need to be positive integers.

… if your answer started with “because in C…”, you've been repeating a good story you heard one time, without ever asking yourself if it's true.

…

The fact of it is this: before pointers, structs, C and Unix existed, at a time when other languages with a lot of resources and (by the standard of the day) user populations behind them were one- or arbitrarily-indexed, somebody decided that the right thing was for arrays to start at zero.

So I found that person and asked him.

His name is Dr. Martin Richards; he's the creator of BCPL, now almost 7 years into retirement; you've probably heard of one of his doctoral students Eben Upton, creator of the Raspberry Pi. I emailed him to ask why he decided to start counting arrays from zero, way back then. He replied that…

As for BCPL and C subscripts starting at zero. BCPL was essentially designed as typeless language close to machine code. Just as in machine code registers are typically all the same size and contain values that represent almost anything, such as integers, machine addresses, truth values, characters, etc. BCPL has typeless variables just like machine registers capable of representing anything. If a BCPL variable represents a pointer, it points to one or more consecutive words of memory. These words are the same size as BCPL variables. Just as machine code allows address arithmetic so does BCPL, so if p is a pointer p+1 is a pointer to the next word after the one p points to. Naturally p+0 has the same value as p. The monodic indirection operator ! takes a pointer as it’s argument and returns the contents of the word pointed to. If v is a pointer !(v+I) will access the word pointed to by v+I. As I varies from zero upwards we access consecutive locations starting at the one pointed to by v when I is zero. The dyadic version of ! is defined so that v!i = !(v+I). v!i behaves like a subscripted expression with v being a one dimensional array and I being an integer subscript. It is entirely natural for the first element of the array to have subscript zero. C copied BCPL’s approach using * for monodic ! and [ ] for array subscription. Note that, in BCPL v!5 = !(v+5) = !(5+v) = 5!v. The same happens in C, v[5] = 5[v]. I can see no sensible reason why the first element of a BCPL array should have subscript one. Note that 5!v is rather like a field selector accessing a field in a structure pointed to by v.

…

So: the technical reason we started counting arrays at zero is that in the mid-1960's, you could shave a few cycles off of a program's compilation time on an IBM 7094. The social reason is that we had to save every cycle we could, because if the job didn't finish fast it might not finish at all and you never know when you're getting bumped off the hardware because the President of IBM just called and fuck your thesis, it's yacht-racing time.

Why Python Array Start at Zero

Dijkstra on Array Index