Comp Lang: Should Array Index Start at 0 or 1?
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# • Java • JavaScript • Perl • Python • Ruby • Emacs 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:
- Why Do Programmers Start Counting at Zero
- By Mike Hoye.
- http://exple.tive.org/blarg/2013/10/22/citation-needed/

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
- Ontology of Programing Languages
- Comp Lang: Why I Hate Exceptions
- Comp Lang: Iterator, Enumerator, Abstraction Went Wrong (2016)
- Comp Lang: Should Array Index Start at 0 or 1?
- Comp Lang: Where does the “main” function in programing languages came from?
- Comp Lang: is the Map Function Functional Programing in Syntax or Semantics
- Comp Lang: Should Map Function Specify Order
- Comp Lang: Abuse of Logic Operators (Short-Circuit) as Control Flow
- Comp Lang: Bit Operators Idiocy
- Comp Lang: Hack of Bitmask as Boolean Parameters
- Comp Lang: Function Dependency
- The Complexity of Java Access Specifiers
- Comp Lang: C Cpp Java Coders Don't Know Scientific Programing Languages
Programing Languages, Array, List, Sequences
- Meaning of List, Array, Vector, Tuple, Slice, in Programing Languages
- Comp Lang: Should Array Index Start at 0 or 1?
- Why Python Array Start at Zero
- Push Pop Array, Front or End
- Why is Array Access Constant Time
- Bjarne Stroustrup: Why you should avoid Linked Lists
- Why Java Array Syntax Sucks
- JavaScript Array Speed vs C Array
- Programing Trick to Avoid Checking Out of Bounds Index for 2d Array