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

CArrays

C Problem Overview


Why does the indexing in an array start with zero in C and not with 1?

C Solutions


Solution 1 - C

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

Solution 2 - C

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.

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

>Who's on first? Zero or one?

People start counting from the number one. The very word first is abbreviated as 1st, which indicates one. This, however, is a very modern notation. The older concepts do not necessarily support this relationship.
In English and French the word first is not derived from the word one, but from an old word for prince, which means foremost. Similarly, The English word second is not derived from the number two but from an old word which means "to follow." Obviously, there is a close relation between third and three, fourth and four, and so on.
These relationships occur in other language families, also. In Hebrew, for example, first is derived from the word head, meaning "the foremost." The Hebrew word for second is derived from the word two, thisrelationship of ordinal and cardinal names holds for all the other numbers.
For a very long time, people have counted from one, not from zero, As a matter of fact, the inclusion of zero as a full-fledged member of the set of all numbers is a relatively modern concept, even though it is one of the most important numbers mathematically. It has many important properties, such as being a multiple of any integer.
A nice mathematical theorem states that for any basis b the first bⁿ positive integers are represented by exactly n digits (leading zeros included). This is true if and only if the count starts with zero (hence, 0 through bⁿ-1), not with one (for 1 through bⁿ).
This theorem is the basis of computer memory ad dressing. Typically, 2ⁿ cells are addressed by an N-bit addressing scheme. A count starting from one rather than zero would cause the loss of either one memory cell or an additional address line. Since either price is too expensive, computer engineers agree to use the mathematical notation that starts with zero. Good for them!
This is probably the reason why all memories start at address-0, even those of systems that count bits from B1 up.
The designers of the 1401 were probably ashamed to have address-0. They hid it from the users and pretended that the memory starts at address-1.
Communication engineers, like most people, start counting from one. They never have to suffer the loss of a memory cell, for example. Therefore, they happily count one-to-eight, not zero-to-seven, as computer people do. ref

Solution 3 - C

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!

Solution 4 - C

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.

Solution 5 - C

Because it made the compiler and linker simpler (easier to write).

Reference:

> "...Referencing memory by an address and an offset is represented directly in hardware on virtually all computer architectures, so this design detail in C makes compilation easier"

and

> "...this makes for a simpler implementation..."

Solution 6 - C

For the same reason that, when it's Wednesday and somebody asks you how many days til Wednesday, you say 0 rather than 1, and that when it's Wednesday and somebody asks you how many days until Thursday, you say 1 rather than 2.

Solution 7 - C

Array index always starts with zero.Let assume base address is 2000. Now arr[i] = *(arr+i). Now if i= 0, this means *(2000+0)is equal to base address or address of first element in array. this index is treated as offset, so bydeafault index starts from zero.

Solution 8 - C

I am from a Java background. I Have presented answer to this question in the diagram below which i have written in a piece of paper which is self explanatory

Main Steps:

  1. Creating Reference
  2. Instantiation of Array
  3. Allocation of Data to array

  • Also note when array is just instantiated .... Zero is allocated to all the blocks by default until we assign value for it
  • Array starts with zero because first address will be pointing to the reference (i:e - X102+0 in image)

enter image description here

Note: Blocks shown in the image is memory representation

Solution 9 - C

It is because the address has to point to the right element in the array. Let us assume the below array:

let arr = [10, 20, 40, 60]; 

Let us now consider the start of the address being 12 and the size of the element be 4 bytes.

address of arr[0] = 12 + (0 * 4) => 12
address of arr[1] = 12 + (1 * 4) => 16
address of arr[2] = 12 + (2 * 4) => 20
address of arr[3] = 12 + (3 * 4) => 24

If it was not zero-based, technically our first element address in the array would be 16 which is wrong as it's location is 12.

Solution 10 - C

The most elegant explanation I've read for zero-based numbering is an observation that values aren't stored at the marked places on the number line, but rather in the spaces between them. The first item is stored between zero and one, the next between one and two, etc. The Nth item is stored between N-1 and N. A range of items may be described using the numbers on either side. Individual items are by convention described using the numbers below it. If one is given a range (X,Y), identifying individual numbers using the number below means that one can identify the first item without using any arithmetic (it's item X) but one must subtract one from Y to identify the last item (Y-1). Identifying items using the number above would make it easier to identify the last item in a range (it would be item Y), but harder to identify the first (X+1).

Although it wouldn't be horrible to identify items based upon the number above them, defining the first item in the range (X,Y) as being the one above X generally works out more nicely than defining it as the one below (X+1).

Solution 11 - C

Suppose we want to create an array of size 5
int array[5] = [2,3,5,9,8]

let the 1st element of the array is pointed at location 100

and let we consider the indexing starts from 1 not from 0.

now we have to find the location of the 1st element with the help of index
(remember the location of 1st element is 100)

since the size of an integer is 4-bit
therefore --> considering index 1 the position would be
size of index(1) * size of integer(4) = 4
so the actual position it will show us is

100 + 4 = 104

which is not true because the initial location was at 100.
it should be pointing to 100 not at 104
this is wrong

now suppose we have taken the indexing from 0
then
position of 1st element should be
size of index(0) * size of integer(4) = 0

therefore -->
location of 1st element is 100 + 0 = 100

and that was the actual location of the element
this is why indexing starts at 0;

I hope it will clear your point.

Solution 12 - C

The technical reason might derive from the fact that the pointer to a memory location of an array is the contents of the first element of the array. If you declare the pointer with an index of one, programs would normally add that value of one to the pointer to access the content which is not what you want, of course.

Solution 13 - C

Try to access a pixel screen using X,Y coordinates on a 1-based matrix. The formula is utterly complex. Why is complex? Because you end up converting the X,Y coords into one number, the offset. Why you need to convert X,Y to an offset? Because that's how memory is organized inside computers, as a continuous stream of memory cells (arrays). How computers deals with array cells? Using offsets (displacements from the first cell, a zero-based indexing model).

So at some point in the code you need (or the compiler needs) to convert the 1-base formula to a 0-based formula because that's how computers deal with memory.

Solution 14 - C

In array, the index tells the distance from the starting element. So, the first element is at 0 distance from the starting element. So, that's why array start from 0.

Solution 15 - C

first of all you need to know that arrays are internally considered as pointers because the "name of array itself contains the address of the first element of array "

ex. int arr[2] = {5,4};

consider that array starts at address 100 so element first element will be at address 100 and second will be at 104 now, consider that if array index starts from 1, so

arr[1]:-

this can be written in the pointers expression like this-

 arr[1] = *(arr + 1 * (size of single element of array));

consider size of int is 4bytes, now,

arr[1] = *(arr + 1 * (4) );
arr[1] = *(arr + 4);

as we know array name contains the address of its first element so arr = 100 now,

arr[1] = *(100 + 4);
arr[1] = *(104);

which gives,

arr[1] = 4;
 

because of this expression we are unable to access the element at address 100 which is official first element,

now consider array index starts from 0, so

arr[0]:-

this will be resolved as

arr[0] = *(arr + 0 + (size of type of array));
arr[0] = *(arr + 0 * 4);
arr[0] = *(arr + 0);
arr[0] = *(arr);

now, we know that array name contains the address of its first element so,

arr[0] = *(100);

which gives correct result

arr[0] = 5;

therefore array index always starts from 0 in c.

reference: all details are written in book "The C programming language by brian kerninghan and dennis ritchie"

Solution 16 - C

Array name is a constant pointer pointing to the base address.When you use arr[i] the compiler manipulates it as *(arr+i).Since int range is -128 to 127,the compiler thinks that -128 to -1 are negative numbers and 0 to 128 are positive numbers.So array index always starts with zero.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNitish PareekView Question on Stackoverflow
Solution 1 - CMassimiliano PelusoView Answer on Stackoverflow
Solution 2 - CAnirudh RamanathanView Answer on Stackoverflow
Solution 3 - CDoug T.View Answer on Stackoverflow
Solution 4 - CBranko DimitrijevicView Answer on Stackoverflow
Solution 5 - CprogrmrView Answer on Stackoverflow
Solution 6 - CR.. GitHub STOP HELPING ICEView Answer on Stackoverflow
Solution 7 - CAmit PrakashView Answer on Stackoverflow
Solution 8 - CDevrathView Answer on Stackoverflow
Solution 9 - CThalaivarView Answer on Stackoverflow
Solution 10 - CsupercatView Answer on Stackoverflow
Solution 11 - Csahil singhView Answer on Stackoverflow
Solution 12 - CRobView Answer on Stackoverflow
Solution 13 - CGianluca GhettiniView Answer on Stackoverflow
Solution 14 - CRishi Raj TandonView Answer on Stackoverflow
Solution 15 - Cakshay chaudhariView Answer on Stackoverflow
Solution 16 - CNiksView Answer on Stackoverflow