Why are array indexes zero-based in most programming languages?
Asked Answered
W

9

6

C++, C#, C, D, Java,... are zero based.

Matlab is the only language I know that begin at 1.

Whittle answered 16/8, 2013 at 22:23 Comment(3)
See programmers.stackexchange.com/q/110804/7043 (sadly, can't close as duplicate - moving it to programmers.SE and then closing it seems silly).Jemina
Matlab is not the only one. Fortran is a good example of 1 based indexing. I think that 0 based indexing is better for general programming, while 1 based indexing is more natural for mathematics.Commines
If you go down the rabbit hole to clearly see how low the overall level of mental and mind sanity in the world societies actually is (last worldwide empirical determination of this level was performed 2020-2023) you will get a glimpse on the actual reason. So if you know for sure how zero-based indexing screws the minds of non-low-level (assembler) programmer and how it screwed your own mind while using Python or Ruby, you stop discussing this subject providing evidence to avoid the "discussion with an ill-minded individual means there are two ill-minded individuals discussing" effect.Ace
R
9

Arrays are zero based in c and c++ as the represent the offset from the beginning of the list of the item.

These two lines have identical result in c.

anArray[3] = 4;
*(anArray +3) = 4; 

The first is the standard indexer the second takes the pointer adds three to id and then dereffrences it. Which is the same as the indexer.

Roasting answered 16/8, 2013 at 22:27 Comment(2)
It will add 3* sizeof(int) assuming this is an int array. This is pointer arithmetic.Compartment
So it's because inner machine representation, not because human thinking and reasoningMaryn
L
4

Well, consider Dijkstra's famous article, Why numbering should start at zero. He argues that numbering should start at 0 because it means that the valid indexes into an array can be described as 0 <= i < N. This is clearly more appealing than 1 <= i < N + 1, on an aesthetic level.

(One could ask, "why not say 0 < i <= N", but he argues against that, too, again for aesthetic reasons.)

Lashkar answered 16/8, 2013 at 22:50 Comment(5)
That argument falls a bit flat when you consider 1 <= i <= N -- which is even more appealing on an aesthetic level, cause now even the relational operators match. :)Peshawar
@cHao: that's the worst option of all, because it means that if you subtract the two endpoints, you no longer get the length of the array. It's actually a cause of bugs in languages whose array slicing functions are inclusive at both ends.Lashkar
Eh. I typically prefer (start, length) myself. You don't as often want "items X through X+N-1" or anything like that; you want "the N items starting at index X". And with that, the length argument all but goes away; nearly every self-respecting language these days has arrays that know their length.Peshawar
@Peshawar Arrays know their length, but slicing is still very useful and common. Perhaps even more common in languages where you don't have to worry about the memory management for the slice.Jemina
@DevinJeanpierre : and remind me how is 0 <= i < N any more appealing than 0 < i <= N - in both scenarios one side of the interval is closed and the other is half-open, just like what Dijkstra wanted. It's beyond arbitrary to declare 0 <= i < N being "appealing" while 0 < i <= N isn't, even though both share the exact same 5 symbols, in different order.Links
G
3

I guess because arrays use pointer arithmetic to refer to some value. Basically arrays have contiguous memory and if you want to refer to 5th element (a[4]) then a + 4 * size of int is performed

Say if you start with 1 then to refer to 5th element you will have to do something like a + (5-1) * size of int

Gebler answered 16/8, 2013 at 22:34 Comment(0)
P
2

Probably "C" got it because it is more efficient. To calculate address of item in 0-based array it is enough to multiple Index by ItemSize, for 1-based array you have to calculate (Index-1)*ItemSize. "C" and then "C++" where most popular languages, so new languages have to follow same rules, it helps to avoid mistakes for those who use C/C++. But this question seems to be offtopic and i guess it will be closed by moderator.

P.S. In Delphi/Pascal strings are 1-based, but for arrays you have to provide range and so you can use what you like.

Portia answered 16/8, 2013 at 22:34 Comment(0)
A
2

Computers speak in binary and decimal digit alignment is simpler using the range 0-9 than 1-10.

The translation from decimal to binary is more direct using zero-based indexing. Binary, decimal, and hexadecimal all naturally start at zero:

decimal:     0
binary:      0000000000000000
hexadecimal: 0

decimal:     1
binary:      0000000000000001
hexadecimal: 1

decimal:     2
binary:      0000000000000010
hexadecimal: 2

...

To start from anywhere else would require an extra calculation (e.g., subtracting one) during compilation to fit binary-native number ranges.

Adsorbate answered 4/4, 2024 at 17:9 Comment(0)
A
2

There are many ONE-based counting/indexing programming languages to choose from after you start to clearly see from own overwhelming evidence how ZERO-based indexing impacts the sanity of your mind :

  -----------------------------------------------------
|  programming   |   ONE    |  Interpreted / |          |
|   language     |  based   |    Compiled /  | REPL ?   |
|     name       |  index   |      Both      |          |
| -------------- | -------- | -------------- | -------- |
| ABAP           |   ONE    | Interpreted    |   ---    |
| Ada            |   ONE    | Compiled       |   ---    |
| Apex           |   ---    | Interpreted    |   ---    |
| Arturo         |   ONE    | Interpreted    |   REPL   |
| AWK            |   ONE    | Interpreted    |   ---    |
| Bash (Shell)   |   ---    | Interpreted    |   REPL   |
| Bend           |   ---    | Compiled       |   ---    |
| C              |   ---    | Compiled       |   ---    |
| C#             |   ---    | Compiled       |   REPL   |
| C++            |   ---    | Compiled       |   ---    |
| Chapel         |   ---    | Compiled       |   ---    |
| COBOL          |   ONE    | Compiled       |   ---    |
| Common·Lisp    |   ---    | Both           |   REPL   |
| Crystal        |   ---    | Compiled       |   ---    |
| D              |   ---    | Compiled       |   ---    |
| Dart           |   ---    | Both           |   REPL   |
| Delphi         |   ONE    | Compiled       |   ---    |
| Elixir         |   ---    | Interpreted    |   REPL   |
| Erlang         |   ONE    | Both           |   REPL   |
| F#             |   ---    | Both           |   REPL   |
| Forth          |   ONE    | Both           |   REPL   |
| Fortran        |   ONE    | Compiled       |   ---    |
| Go             |   ---    | Compiled       |   ---    |
| Groovy         |   ---    | Both           |   REPL   |
| Haskell        |   ---    | Both           |   REPL   |
| Idris          |   ---    | Both           |   REPL   |
| Java           |   ---    | Compiled       |   ---    |
| JavaScript     |   ---    | Interpreted    |   REPL   |
| Julia          |   ONE    | Both           |   REPL   |
| Kotlin         |   ---    | Both           |   REPL   |
| LabVIEW        |   ONE    | Compiled       |   ---    |
| Lisp           |   ---    | Both           |   REPL   |
| Lobster        |   ---    | Compiled       |   ---    |
| Lua            |   ONE    | Interpreted    |   REPL   |
| MATLAB         |   ONE    | Interpreted    |   REPL   |
| Meow5          |   ---    | Interpreted    |   REPL   |
| Mercury        |   ONE    | Compiled       |   ---    |
| Nim            |   ---    | Compiled       |   ---    |
| Objective-C    |   ---    | Compiled       |   ---    |
| Pascal         |   ONE    | Compiled       |   ---    |
| Perl           |   ---    | Interpreted    |   REPL   |
| PHP            |   ---    | Interpreted    |   REPL   |
| PL/SQL         |   ONE    | Interpreted    |   ---    |
| Pony           |   ---    | Compiled       |   ---    |
| Prolog         |   ONE    | Both           |   REPL   |
| Python         |   ---    | Interpreted    |   REPL   |
| R              |   ONE    | Interpreted    |   REPL   |
| Racket         |   ---    | Interpreted    |   REPL   |
| Red            |   ---    | Both           |   REPL   |
| Ruby           |   ---    | Interpreted    |   REPL   |
| Rust           |   ---    | Compiled       |   ---    |
| SAS            |   ONE    | Both           |   ---    |
| sed            |   ONE    | Interpreted    |   ---    |
| Scala          |   ---    | Both           |   REPL   |
| Scheme         |   ---    | Interpreted    |   REPL   |
| Smalltalk      |   ONE    | Both           |   REPL   |
| SQL            |   ONE    | Interpreted    |   REPL   |
| Swift          |   ---    | Both           |   REPL   |
| TypeScript     |   ---    | Compiled       |   ---    |
| V              |   ---    | Compiled       |   ---    |
| VBA            |   ONE    | Interpreted    |   ---    |
| VBScript       |   ONE    | Interpreted    |   ---    |
| Visual Basic   |   ---    | Both           |   ---    |
| Zig            |   ---    | Compiled       |   ---    |
  -----------------------------------------------------
Ace answered 1/6, 2024 at 19:38 Comment(2)
Not Mercury dev, but do you have source for Mecury being 1 based?Sphere
@Sphere : in Mercury there is no indexing in usual sense so you are free to decide the index base yourself. In the context of the question it makes therefore sense to indicate the possibility of having one based indexing even if the question about the index base does not make sense in context of this specific programming language.Ace
R
1

I guess it has mostly historical reasons, new languages just try to use the existing convention which programmers are familiar with.

Older languages from which this rule originated were close to the metal, and an index is really the distance from the starting element, hence 0 makes sense for the first element.

Resupine answered 16/8, 2013 at 22:27 Comment(0)
L
0

Ironically, it's the languages that specialize in numeric computing - MATLAB, Mathematica, WolframAlpha, R, Julia etc that all go for 1-based indexing.

Not to mention that seq, jot, cut, head, and tail, are ALL 1-based.

Say a calendar array of months x days (with a handful of invalid cells in order to make the array rectangular), the syntax for accessing July 19th when it's 1-based indexing is intuitive to a point it's almost self-explanatory :

calendar[7][19]

... now compare that to 0-based paradigms, which involve accessing

calendar[6][18]

to obtain the value of July 19th …… argh

Links answered 2/6, 2024 at 22:40 Comment(0)
K
-3

Because there are 10 integers 0..9

Krol answered 16/8, 2013 at 23:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.