Comp Lang: Should Array Index Start at 0 or 1?

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. For example, • C • C++ • C# • JavaJavaScriptPerlPythonRubyEmacs Lisp .

Which language's array index starts at 1?

Math languages start at 1: • Wolfram Language • APL • Julia • Lua • Erlang • FORTRAN • SASL • MATLAB • Smalltalk .

Should Array Index Start at 0 or 1?

Counting should start at 1, and ending index should be inclusive. Because that's just easier to work with.

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 countless 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

Here is a excerpt from a most well-researched essay:

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…

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

Programing Language Design

Programing Languages, Array, List, Sequences