C++, C#, C, D, Java,... are zero based.
Matlab is the only language I know that begin at 1.
C++, C#, C, D, Java,... are zero based.
Matlab is the only language I know that begin at 1.
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.
3* sizeof(int)
assuming this is an int
array. This is pointer arithmetic. –
Compartment 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.)
1 <= i <= N
-- which is even more appealing on an aesthetic level, cause now even the relational operators match. :) –
Peshawar (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 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 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
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.
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.
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 | --- |
-----------------------------------------------------
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.
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
© 2022 - 2025 — McMap. All rights reserved.