This is a text-version of the video we published on YouTube.
A wife asked a developer, “Who is more important to you, me or programming?” “Honey”, he replied, “of course you take first place.”
The fact that programmers start counting from zero instead of from one is so well-known it became a meme. But do you know why is it so? There are two key reasons, both of which made a lot of sense in the C programming language, that solidified this convention.
img_1.png
The first one relates to memory consumption. In the early days of programming, we respected our memory much more, and wherever possible, we used as small representations for numbers as possible. So we used short or byte if that was enough, and we used an unsigned type if we needed no negative numbers. We still do that in some applications, but in the past, it was an industry standard because memory was much more expensive. Getting back to indices, since we have no way to represent positive numbers without 0, starting from 1 would mean wasting one possible value. For instance, if you use unsigned byte and start indexing from 0, you have 256 possible indices, but if you started from 1 you had only 255, which can make a difference.
(tabelka może wjechać niemalże od razu, formuła po “The formula for the”)
How to refer
Address in memory
Actual value in memory
arr[0] = arr
68
1100101010…
arr[1] = arr + 1 * 4
72
0101010101…
arr[2] = arr + 2 * 4
76
0101010101…
arr[3] = arr + 3 * 4
80
0101010010…
img.png
The second reason concerns how our memory is managed. Traditionally, the standard way to store a collection of elements was to use an array. An array is a reservation for a certain number of objects, one after another, in memory. For instance, if you create an array of int of size 100, that reserves 100 times int size bytes in memory. We referred to those elements by their indices. The formula for the address of an element at a given index is much simpler if we start our indices at 0; the formula is then array address + index times element type size. For instance, if the array address is 64, you look for the element at index 3, and you know elements are 4 bytes, you can find it at the address 76. This formula would be slightly more complicated if we started indexing from 1.
However, it is worth mentioning that not all programming languages start indexing from 0. The most popular counterexamples include MATLAB, Lua, and Julia, which start at 1. Other languages still index from 0, as it is a deeply ingrained convention, and every language designer fears that indexing from 1 would cause unnecessary confusion.
Before I end, I want to note that indexing from zero isn’t that uncommon outside programming. A ground floor is the zeroth floor; a newborn is zero years old; the first hour is zero on the 24-hour clock; there is the zeroth law of thermodynamics and the zeroth derivative; and in one hotel, I even found a zeroeth room number.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.