8

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

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

3
  • 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). Commented Aug 16, 2013 at 22:26
  • 1
    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. Commented Jan 13, 2014 at 21:25
  • 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. Commented Jun 1, 2024 at 19:27

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It will add 3* sizeof(int) assuming this is an int array. This is pointer arithmetic.
So it's because inner machine representation, not because human thinking and reasoning
5

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.)

5 Comments

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. :)
@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.
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.
@cHao 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.
@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.
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

Comments

3

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.

Comments

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.

Comments

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

2 Comments

Not Mercury dev, but do you have source for Mecury being 1 based?
@nawfal : 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.
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.

3 Comments

yeah and I've seen Javascript regex like /3|5|8|10/ to match the months with 30 days for values returned by JS's functions because of their 0-based indexing even for calendar months, but then need a differently set of /[0]?[469]|11/ if they wanna manually parse free form date/datetime strings. arghhhhhhhh
Another weird aspect is that the boolean "truthiness" of existing and valid indices of an object (string, array, list etc.) differ from each other, leading to very verbose python codes for try/catch ValueError when using the .index( ) methods to check for existence of something among a larger collection. With 1-based indexing, existing and valid indices are TRUE in boolean context, so index() functions simply return 0 (i.e. False) to represent non-existence.
Personally I think python3, and by extension, its Fascist Dictator for Life, is insane for throwing an error for something as mundane as not locating something among a large collection, and equating the seriousness of this outcome with something like division by zero.
1

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

Comments

-4

Because there are 10 integers 0..9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.