76

C programming language is known as a zero index array language. The first item in an array is accessible using 0. For example double arr[2] = {1.5,2.5} The first item in array arr is at position 0. arr[0] === 1.5 What programming languages are 1 based indexes?

I've heard of the these languages start at 1 instead of 0 for array access: Algol, Matlab, Action!, Pascal, Fortran, Cobol. Is this complete?

Specificially, a 1 based array would access the first item with 1, not zero.

gtzilla
  • 1,265
  • 1
  • 16
  • 21
Alec Jacobson
  • 6,032
  • 5
  • 51
  • 88
  • 8
    This should be a wiki – Austin Salonen Sep 30 '09 at 18:10
  • 53
    Why are people voting to close for "not a real question"? It is quite clearly a real question. It may not be the best one the site has ever seen, and it may be close-worthy, but certainly not because it's "not a real question." – Chris Lutz Sep 30 '09 at 18:11
  • 2
    Pretty much almost everything from before C, as the whole Zero-indexed thing started out as an assmebler/C implementation artifact (as real humans do not count from Zero). So Basic(s), COBOL, Fortran(s), and most of the languages that are descended from them. – RBarryYoung Sep 30 '09 at 18:14
  • Could you clarify the question, please? – Narek Sep 30 '09 at 18:17
  • Pascal is not necessarily 1-indexed, you get to choose the start and end indices. You can declare ARRAY[0..5] or ARRAY[1..6] to create an array of 6 elements. – Dan Dyer Sep 30 '09 at 18:18
  • 1
    I guess the title isn't correctly formatted as a question, but the body certainly is. How is this not a real question? – Samantha Branham Sep 30 '09 at 18:30
  • I voted "too localized", but the majority wins. Either way the question is pretty silly and useless. – Ed S. Sep 30 '09 at 18:32
  • @RBarryYoung - I sense some bitterness. I have a little right to be bitter too - C questions aren't as common as you think, and C is sorely underrepresented, along with my native language (Perl). And no, no one would let "What's the best junk food for Haskell programmers" stay open more than a minute. – Chris Lutz Sep 30 '09 at 18:33
  • @Ed Swangren - I can buy that one, and I'm not voting to reopen because I largely agree with you. – Chris Lutz Sep 30 '09 at 18:34
  • @Ed Swangren - what exactly does "too localized" mean ? That the question will interest only a small group of people or ? – Rook Sep 30 '09 at 19:25
  • To OP: Perhaps the question would be better defined if it states "What programming languages are 1-indexed if the index is not exlicitly defined (i.e. by default) ?" – Rook Sep 30 '09 at 19:28
  • I wish I could upvote my own comment. 9 upvotes on a comment asking "Why are you voting to close for not being a real question" and there are _still_ 2 votes to close for not being a real question! Does anyone who cast any of those votes (and I can name about 4 of you) care to explain why you cast them? Maybe I'd understand if you just explained it, but as it is this is getting ridiculous. – Chris Lutz Oct 01 '09 at 02:42
  • 7
    Chris Lutz: RE: Bitter; not really bitter yet, lets just say "cranky and disgruntled", esp. w/ the Close-Voting on this site. RE: "Food for Programming"; you know I used this name as a joke, but here in fact is a topic almost identical to this that has been open for a year: http://stackoverflow.com/questions/92257/programmers-food. This is what disgruntles me, the close-voting here has *nothing* to do with real programming questions and *everything* to do with a certain programmer's subculture that is coddled by this site. A subculture that is both parochial and close-minded. – RBarryYoung Oct 01 '09 at 14:51
  • @RBarryYoung - ther' be some truth in that ! – Rook Oct 01 '09 at 19:42
  • @RBarryYoung - That particular question got the Jeff Atwood seal of approval in a podcast (http://blog.stackoverflow.com/2008/09/podcast-23/) for some reason. I disagree with that particular question, but agree with the general consensus of "a little fun won't hurt." However, in the end this question won. – Chris Lutz Oct 06 '09 at 18:28
  • 1
    Heh, interesting question! I imagine there must be some in the "scripting language targeted at end users" field, but I don't know any. – Pekka Oct 23 '10 at 09:58
  • The com interop of MS Office in general prefers 1 based index, for instance: `Excel.Workbook wB = excelApp.Workbooks[1];` to get the first workbook. Otherwise `Lua` and `Smalltalk` are two among the mainstream ones.. – nawfal Feb 26 '13 at 11:30
  • This isn't an opinion question. It's a factual question on language structures. This needs to be un-moderated. Moderation should not be allow to score any points. Like down vote, close as off-topic is 90% wrong for anyone who on bleeding edge – gtzilla Jul 19 '18 at 20:59
  • In fact starting at 1 is the most natural way, you start counting your fingers at one, mathematicians, physicists and engineers start counting and indexing at one, only some programmers start at 0. – skan Feb 23 '20 at 19:57

28 Answers28

63

A list can be found on wikipedia.

ALGOL 68
APL
AWK
CFML
COBOL
Fortran
FoxPro
Julia
Lua
Mathematica
MATLAB
PL/I
Ring
RPG
Sass
Smalltalk
Wolfram Language
XPath/XQuery
nehem
  • 12,775
  • 6
  • 58
  • 84
LJM
  • 6,284
  • 7
  • 28
  • 30
  • Seems the BASIC entry in that list is not completely correct. The Base index of an array was specifiable. – Yannick Motton Sep 30 '09 at 18:20
  • 3
    Note that it says "default base index", so that part is correct. What's incorrect however is that default base index in BASIC is still 0, not one - so I fixed that. – Pavel Minaev Sep 30 '09 at 19:38
  • 5
    Actually, BASIC varied a *lot*, particularly on this point. – RBarryYoung Oct 01 '09 at 03:42
  • 2
    I was talking about 'Specifiable base index' which is possible by either `OPTION BASE {0 | 1}`, either by specifying a lower bound when declaring `DIM MyArray(-19 To 20) As Integer`. The default base index in the DOS BASICs is still 1. – Yannick Motton Oct 02 '09 at 13:52
  • http://en.wikipedia.org/wiki/Comparison%5Fof%5Fprogramming%5Flanguages%5F%28array%29#Array_system_cross-reference_list is the link – Grzegorz Oledzki Oct 02 '09 at 13:57
  • 9
    @Yannick: You do know Wikipedia is editable by anyone, right? Why complain about inaccuracies; just correct them! – DisgruntledGoat Oct 06 '09 at 18:17
  • 1
    Regarding "wiki correctness". _PHP_ doesn't have arrays, everything is map; not a PHP dev, so not sure what kind of map, but it uses `key` to `value` mapping under the hood – ST3 Jun 14 '17 at 14:27
  • 3
    @ST3, that might be how it works under the hood, but PHP still calls them arrays and their basic behavior (using them without specifying keys) acts like arrays. – Tynach Aug 14 '17 at 00:44
20

Fortran starts at 1. I know that because my Dad used to program Fortran before I was born (I am 33 now) and he really criticizes modern programming languages for starting at 0, saying it's unnatural, not how humans think, unlike maths, and so on.

However, I find things starting at 0 quite natural; my first real programming language was C and *(ptr+n) wouldn't have worked so nicely if n hadn't started at zero!

Adrian Smith
  • 17,236
  • 11
  • 71
  • 93
  • 13
    +1: but it's really for your dad who started programming in Fortran at about the same time as I did. I expect that when he taught you to count, he taught you something like 1 cow, 2 cows, 3 cows ... Now who's unnatural ! – High Performance Mark Oct 23 '10 at 10:11
  • 6
    @High Performance Mark The cows are unnatural of course. They should start at 0 cows. – abel Oct 23 '10 at 10:35
  • 3
    When counting items, not even C programmers say there are 0 items, if there's 1 of them! But the 1st item can validly be at index 0 nonetheless. –  Oct 23 '10 at 10:47
14

A pretty big list of languages is on Wikipedia under Comparison of Programming Languages (array) under "Array system cross-reference list" table (Default base index column)

This has a good discussion of 1- vs. 0- indexed and subscriptions in general

To quote from the blog:

EWD831 by E.W. Dijkstra, 1982.

When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.

Remark:: Many programming languages have been designed without due attention to this detail. In FORTRAN subscripts always start at 1; in ALGOL 60 and in PASCAL, convention c) has been adopted; the more recent SASL has fallen back on the FORTRAN convention: a sequence in SASL is at the same time a function on the positive integers. Pity! (End of Remark.)

Community
  • 1
  • 1
DVK
  • 126,886
  • 32
  • 213
  • 327
  • 5
    A lot of non-cs engineers would (and do) disagree with some of Dijkstra's views, in regards that that in away-from-keyboard mathematics 1 is still the "default" first element of an array. – Rook Sep 30 '09 at 19:10
  • Also, in fortran an array subscript doesn't always start at 1. – Rook Sep 30 '09 at 19:11
  • 12
    Dijkstra is of course presenting a very biased view hear. Consider that most people would describe the range of the 1-based arrays as "1 ≤ i ≤ N", which is a heck of a lot nicer than "0 ≤ i ≤ N-1". – RBarryYoung Oct 01 '09 at 03:47
  • Yeah, Dijkstra's view is very biased, and besides, assuming of a different paradigm than how people code nowadays - nowadays, there are a lot less arguments against 1-based indexing than there used to be, even if just because of for-in. – Llamageddon May 01 '15 at 13:30
12

Also in Ada you can define your array indices as required:

A : array(-5..5) of Integer;       -- defines an array with 11 elements
B : array(-1..1, -1..1) of Float;  -- defines a 3x3 matrix

Someone might argue that user-defined array index ranges will lead to maintenance problems. However, it is normal to write Ada code in a way which does not depend on the array indices. For this purpose, the language provides element attributes, which are automatically defined for all defined types:

A'first   -- this has the value -5
A'last    -- this has the value +5
A'range   -- returns the range -5..+5 which can be used e.g. in for loops
Schedler
  • 1,403
  • 9
  • 8
11

Fortran, Matlab, Pascal, Algol, Smalltalk, and many many others.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
11

You can do it in Perl

$[ = 1;  # set the base array index to 1

You can also make it start with 42 if you feel like that. This also affects string indexes.

Actually using this feature is highly discouraged.

Thilo
  • 257,207
  • 101
  • 511
  • 656
7

JDBC (not a language, but an API)

String x = resultSet.getString(1);  // the first column
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 7
    +1 That's total madness, it gets me every time, due to the fact that everything else in Java is zero-based! – Adrian Smith Oct 23 '10 at 10:37
  • 2
    That's probably because SQL also use 1 based indices e.g. `ORDER BY 1` (first column) or `SUBSTRING(name, 2)` (start from the 2nd character) – Alex Jasmin Oct 23 '10 at 12:50
  • Note that Hibernate, a popular ORM for Java that (usually) sits on top of JDBC has setString methods where the index is 0-based. Confusing. – Thilo Dec 02 '10 at 00:00
7

Erlang's tuples and lists index starting at 1.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
6

Lua - disappointingly

matja
  • 4,014
  • 3
  • 23
  • 29
  • 2
    What's wrong with lua having a 1-based index? i tmakes it more friendly for beginners to programming. – RCIX Oct 11 '09 at 08:00
4

Found one - Lua (programming language)

Check Arrays section which says -

"Lua arrays are 1-based: the first index is 1 rather than 0 as it is for many other programming languages (though an explicit index of 0 is allowed)"

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
4

VB Classic, at least through

Option Base 1
Dario
  • 48,658
  • 8
  • 97
  • 130
  • Base 1? Please not, unary is sooo unwieldy... –  Oct 23 '10 at 10:55
  • VB.NET as well — the only language where `Dim x(10)` creates *11* elements… – Josh Lee Oct 23 '10 at 14:48
  • @jleedev: Not exactly - VB.NET-Arrays are `0`-based, and the number declares the upper bound (valid indices range from `0` to `10`). This fits well since VB's For loops are *inclusive*. – Dario Oct 23 '10 at 15:01
4

Strings in Delphi start at 1.

(Static arrays must have lower bound specified explicitly. Dynamic arrays always start at 0.)

gabr
  • 26,580
  • 9
  • 75
  • 141
3

ColdFusion - even though it is Java under the hood

andrewWinn
  • 1,786
  • 2
  • 14
  • 28
3

Ada and Pascal.

Dave
  • 4,546
  • 2
  • 38
  • 59
  • I don't think Pascal should be included as it uses both 0 and 1 depending on what's being looked at. Strings are 1, but arrays are 0. – Tom A Sep 30 '09 at 18:19
  • 1
    It's been a while, but I believe you can use any index you like as the start/end of an array in Pascal, including negative values. ARRAY[-5..-2] would create an array of 4 elements. – Dan Dyer Sep 30 '09 at 19:06
3

PL/SQL. An upshot of this is when using languages that start from 0 and interacting with Oracle you need to handle the 0-1 conversions yourself for array access by index. In practice if you use a construct like foreach over rows or access columns by name, it's not much of an issue, but you might want the leftmost column, for example, which will be column 1.

Gaius
  • 2,556
  • 1
  • 24
  • 43
3

Indexes start at one in CFML.

davidcl
  • 1,187
  • 8
  • 23
3

The entire Wirthian line of languages including Pascal, Object Pascal, Modula-2, Modula-3, Oberon, Oberon-2 and Ada (plus a few others I've probably overlooked) allow arrays to be indexed from whatever point you like including, obviously, 1.

Erlang indexes tuples and arrays from 1.

I think—but am no longer positive—that Algol and PL/1 both index from 1. I'm also pretty sure that Cobol indexes from 1.

Basically most high level programming languages before C indexed from 1 (with assembly languages being a notable exception for obvious reasons – and the reason C indexes from 0) and many languages from outside of the C-dominated hegemony still do so to this day.

JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
2

There is also Smalltalk

Dawie Strauss
  • 3,706
  • 3
  • 23
  • 26
2

Visual FoxPro, FoxPro and Clipper all use arrays where element 1 is the first element of an array... I assume that is what you mean by 1-indexed.

PilotBob
  • 3,107
  • 7
  • 35
  • 52
2

I see that the knowledge of fortran here is still on the '66 version.

Fortran has variable both the lower and the upper bounds of an array.

Meaning, if you declare an array like:

real, dimension (90) :: x

then 1 will be the lower bound (by default).

If you declare it like

real, dimension(0,89) :: x

then however, it will have a lower bound of 0.

If on the other hand you declare it like

real, allocatable :: x(:,:)

then you can allocate it to whatever you like. For example

allocate(x(0:np,0:np))

means the array will have the elements

x(0, 0), x(0, 1), x(0, 2 .... np)
x(1, 0), x(1, 1), ...
.
.
.
x(np, 0) ...

There are also some more interesting combinations possible:

real, dimension(:, :, 0:) :: d
real, dimension(9, 0:99, -99:99) :: iii

which are left as homework for the interested reader :)

These are just the ones I remembered off the top of my head. Since one of fortran's main strengths are array handling capabilities, it is clear that there are lot of other in&outs not mentioned here.

Rook
  • 60,248
  • 49
  • 165
  • 242
2

Nobody mentioned XPath.

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
  • +1. See also http://stackoverflow.com/questions/3319341/why-do-indexes-in-xpath-start-with-1-and-not-0 – Thilo Apr 08 '11 at 12:50
2

Mathematica and Maxima, besides other languages already mentioned.

Lorenzo Stella
  • 242
  • 2
  • 11
2

informix, besides other languages already mentioned.

Javi Moya
  • 193
  • 1
  • 4
  • 11
2

Basic - not just VB, but all the old 1980s era line numbered versions.

Richard

winwaed
  • 7,645
  • 6
  • 36
  • 81
1

FoxPro used arrays starting at index 1.

Robert Cartaino
  • 27,494
  • 6
  • 45
  • 67
1

dBASE used arrays starting at index 1.

Arrays (Beginning) in dBASE

Robert Cartaino
  • 27,494
  • 6
  • 45
  • 67
1

RPG, including modern RPGLE

Internet man
  • 1,115
  • 3
  • 13
  • 21
0

Although C is by design 0 indexed, it is possible to arrange for an array in C to be accessed as if it were 1 (or any other value) indexed. Not something you would expect a normal C coder to do often, but it sometimes helps.

Example:

#include <stdio.h>
int main(){
    int zero_based[10];
    int* one_based;
    int i;
    one_based=zero_based-1;

    for (i=1;i<=10;i++) one_based[i]=i;
    for(i=10;i>=1;i--) printf("one_based[%d] = %d\n", i, one_based[i]);
    return 0;
}
MAK
  • 26,140
  • 11
  • 55
  • 86
  • 5
    The line where you subtract `1` from `zero_based` is undefined behavior according to ISO C - it's not legal to shift pointer to before the first element in an array. A conformant implementation may insert out-of-bounds checks that would be triggered by your code, for example. – Pavel Minaev Sep 30 '09 at 19:36
  • That's true. I'm just posting this as an interesting curiosity rather than a serious programming technique. In gcc nothing bad happens as long as one stays within the bounds of the original array. – MAK Oct 01 '09 at 06:37