3

Possible Duplicate:
Why does the indexing start with zero in 'C'?

Why do prevailing programming languages like C use array starting from 0? I know some programming languages like PASCAL have arrays starting from 1. Are there any good reasons for doing so? Or is it merely a historical reason?

Community
  • 1
  • 1
Strin
  • 677
  • 9
  • 15
  • 1
    You began life counting from zero. The day you were born, you were 0 years old. Later in life, social conventions changed your perspective and you started counting from 1. – William Pursell Feb 07 '12 at 10:31
  • [This might be helpful to answer your question: http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-count-from-zero/](http://www.johndcook.com/blog/2008/06/26/why-computer-scientists-count-from-zero/) – noplacetoh1de Feb 07 '12 at 10:28

5 Answers5

3

Because you access array elements by offset relative to the beginning of the array.

First element is at offset 0.

Later more complex array data structures appeared (such as SAFEARRAY) that allowed arbitrary lower bound.

GSerg
  • 76,472
  • 17
  • 159
  • 346
3

In C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language.

However, Dijkstra explains why we should index from 0. This is a problem on how to denote a subsequence of natural numbers, say for example 1,2,3,...,10. We have four solutions available:

a. 0 < i < 11

b. 1<= i < 11

c. 0 < i <= 10

d. 1 <= i <= 10

Dijkstra argues that the proper notation should be able to denote naturally the two following cases:

  1. The subsequence includes the smallest natural number, 0
  2. The subsequence is empty

Requirement 1. leaves out a. and c. since they would have the form -1 < i which uses a number not lying in the natural number set (Dijkstra says this is ugly). So we are left with b. and d. Now requirement 2. leaves out d. since for a set including 0 that is shrunk to the empty one, d. takes the form 0 <= i <= -1, which is a little messed up! Subtracting the ranges in b. we also get the sequence length, which is another plus. Hence we are left with b. which is by far the most widely used notation in programming now.

Now you know. So, remember and take pride in the fact that each time you write something like

for( i=0; i<N; i++ ) {
    sum += a[i];
}

you are not just following the rules of language notation. You are also promoting mathematical beauty!

here

Frankline
  • 40,277
  • 8
  • 44
  • 75
  • +1 for quoting Dijkstra. Here's a link to the article: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html – Dhara Aug 28 '12 at 12:48
2

In assembly and C, arrays was implemented as memory pointers. There the first element was stored at offset 0 from the pointer.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
1

In C arrays are tied to pointers. Array index is a number that you add to the pointer to the array's initial element. This is tied to one of the addressing modes of PDP-11, where you could specify a base address, and place an offset to it in a register to simulate an array. By the way, this is the same place from which ++ and -- came from: PDP-11 provided so-called auto-increment and auto-decrement addressing modes.

P.S. I think Pascal used 1 by default; generally, you were allowed to specify the range of your array explicitly, so you could start it at -10 and end at +20 if you wanted.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Suppose you can store only two bits. That gives you four combinations: 00 10 01 11 Now, assign integers to those 4 values. Two reasonable mappings are:

00->0
01->1
10->2
11->3

and

11->-2 
10->-1 
00->0 
01->1

(Another idea is to use signed magnitude and use the mapping: 11->-1 10->-0 00->+0 01->+1)

It simply does not make sense to use 00 to represent 1 and use 11 to represent 4. Counting from 0 is natural. Counting from 1 is not.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Nope. Counting from 0 is natural for computing, *not* for humans. Did you really being counting sequences from 0 when you were at school? Or did the class always begin with 1? – adelphus Feb 07 '12 at 10:42
  • @adelphus, did you read the fist paragraph in my answer? It is an explanation of why counting from zero is natural in computing. This is a forum about computing. Perhaps I should add the phrase "in computing" in the answer, but it's pretty clear from context. – William Pursell Feb 07 '12 at 11:00
  • Saying something is "natural" usually implies that it matches a scenario in real-life. Making the first element "zero" does not match real-life (or do you really say "Pass me the zeroeth item please"?!) I suspect the reason the question was asked because the system is not intuitive for new developers. – adelphus Feb 07 '12 at 11:10