Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the indexing start with zero in 'C'?

Tags:

arrays

c

People also ask

Why does the indexing of array start with zero in C?

An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0 index element. So *(arr+i) means the element at i distance from the first element of the array. So array index starts from 0 as initially i is 0 which means the first element of the array.

Does C indexing start at 0 or 1?

That's why most languages like C++, PYTHON, JAVA use arrays starting with index 0 and rarely languages like Lua arrays starting with index 1.

Are index in C always start at?

Array index always starts from 0.

Does list indexing start 0?

Note. python lists are 0-indexed. So the first element is 0, second is 1, so on.


In C, the name of an array is essentially a pointer [but see the comments], 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].

For more info:

http://developeronline.blogspot.com/2008/04/why-array-index-should-start-from-0.html


This question was posted over a year ago, but here goes...


About the above reasons

While Dijkstra's article (previously referenced in a now-deleted answer) makes sense from a mathematical perspective, it isn't as relevant when it comes to programming.

The decision taken by the language specification & compiler-designers is based on the decision made by computer system-designers to start count at 0.


The probable reason

Quoting from a Plea for Peace by Danny Cohen.

  • IEEE Link
  • IEN-137

For any base b, the first b^N non-negative integers are represented by exactly N digits (including leading zeros) only if numbering starts at 0.

This can be tested quite easily. In base-2, take 2^3 = 8 The 8th number is:

  • 8 (binary: 1000) if we start count at 1
  • 7 (binary: 111) if we start count at 0

111 can be represented using 3 bits, while 1000 will require an extra bit (4 bits).


Why is this relevant

Computer memory addresses have 2^N cells addressed by N bits. Now if we start counting at 1, 2^N cells would need N+1 address lines. The extra-bit is needed to access exactly 1 address. (1000 in the above case.). Another way to solve it would be to leave the last address inaccessible, and use N address lines.

Both are sub-optimal solutions, compared to starting count at 0, which would keep all addresses accessible, using exactly N address lines!


Conclusion

The decision to start count at 0, has since permeated all digital systems, including the software running on them, because it makes it simpler for the code to translate to what the underlying system can interpret. If it weren't so, there would be one unnecessary translation operation between the machine and programmer, for every array access. It makes compilation easier.


Quoting from the paper:

enter image description here


Because 0 is how far from the pointer to the head of the array to the array's first element.

Consider:

int foo[5] = {1,2,3,4,5};

To access 0 we do:

foo[0] 

But foo decomposes to a pointer, and the above access has analogous pointer arithmetic way of accessing it

*(foo + 0)

These days pointer arithmetic isn't used as frequently. Way back when though, it was a convenient way to take an address and move X "ints" away from that starting point. Of course if you wanted to just stay where you are, you just add 0!


Because 0-based index allows...

array[index]

...to be implemented as...

*(array + index)

If index were 1-based, compiler would need to generate: *(array + index - 1), and this "-1" would hurt the performance.