14
Why the array does not start with 1? There is some technical reason to have adopted 0?
14
Why the array does not start with 1? There is some technical reason to have adopted 0?
16
It has a mathematical basis. If the first natural number is zero, why computers should do it differently?
You may be thinking, "in math matrices begin at 1". Yes, but this runs away from the pattern that mathematics itself has set upon a more fundamental concept that begins with zero.
Nothing stops a person starting his matrix with zero index, it is almost a fluke that in school we learn to start with 1. Of course, it is a universal convention of mathematics, but that does not bring bad consequences if it is not adopted.
In computers it was possible to correct this and make the use more linear.
But there’s one more mathematical reason:
to a b base, the first
b^N
non-negative integer with exponent N is represented binarially by N digits in all situations, if starting with 0.
As demonstrated by a publication in the EIT.
Using base 2 which is the fundamental number of computation: 2^3 = 8
. So the eighth number is:
So computers, before languages, were built like this. I explain.
The memory has 2 N positions, where N is the number of bits supported. If you started from 1 to calculate positions you would have to add 1 to reach all positions, or do N + 1 to represent all possible positions, or make the last address inaccessible. Bad solutions. Starting with 0 solves this issue in a simple and elegant way.
If you think about it it makes sense that languages are like this because computers were built starting everything from 0.
Not all languages use this form. Some even allow each sequence to determine which number you want to start with. What makes it easy to create bugs when some sequences with different boundaries interact with each other. These languages are usually less efficient because there is an impedance between it and the computer.
C was one that used this form, and most languages follow what C does, even to facilitate interoperability with the open language of computing. C was created to be a Assembly portable and more readable. Had to do close to how the computer works.
Starting at 0 was important because the array actually is the address where the sequence starts, and the indexes are calculated based on that address (address + index * element size), so array[0]
is array + 0
. If I started at 1 I’d have to:
Both are bad solutions, even more so in the old less powerful computers. That alone is a good reason to start with 0.
In C arrays are usually just pointers. And it is not known the size of it beforehand. So when will sweep the array, in general, do not need to do "size minus 1". In C it makes more sense to start from 0.
There’s a article by Edsger Dijkstra* who questions this. He thinks this option was a mistake.
Dijkstra talks about aesthetics. He considers it "prettier" to start with 1 and demonstrates why. Other people find it more beautiful to start from the 0. Aesthetics is based on opinions.
*Yes, one of the greatest computers of all time did not use computers :)
This response was very much based on a reply community wiki of OS.
Browser other questions tagged c array characteristic-language mathematics computer-science
You are not signed in. Login or sign up in order to post.
– Jefferson Quesado
Is zero natural or not? Some links (in English) that explain that it can be a matter of taste/subject easier to explain with or without, or ISO 80000-2: https://www.youtube.com/watch?v=7yXeX7ccq9Y&feature=youtu.be&t=250 https://math.stackexchange.com/questions/283/is-0-a-natural-number https://en.wikipedia.org/wiki/Natural_number
– Mateus -- O Schroeder