Why do programmers count from zero?

Recently I came across a posting that was explaining "sort" to someone else. The example given was sorting a list of multiple columns:


abc     123
jat     458
cse     247
 

and because they wanted to sort on the second column, the command was given as "sort +1".

It was the explanation that caught my eye:

Remember that the first word is 0, not 1 (this is due to really old legacy issues that also crept into the C language, btw)

I thought that was a pretty funny way to explain it. Using the word "legacy" makes it sound like it had to be done this way because something older would break if it were not, or perhaps that you used to have to do it this way but now that computers are more powerful it's a silly artifact. Not really true: it never HAD to be done that way, but programmers think that way for good reason.


We're not programmers

When I started learning about computers, most of my peers in the field knew at least a little bit about programming. You really had to if you wanted to be able to do much of anything. That's not true today: many tech support people know nothing about programming, sometimes not even simple scripting. Even though I understand that they really don't absolutely need that knowledge to do their jobs, it still amazes me that so few have it, and it surprises me even more that so many are apt to have a very negative attitude toward the subject.

But given that, it's not surprising that these people don't know why programmers count from zero.

It's all about storage

We're going to dive right into the gory guts of that sort command and see why that starts counting from zero. You don't need to know a thing about programming, you just need to think about how things can be stored in memory. That's what it's really all about: putting things into memory, manipulating them in some way, and letting you know the results. That is programming.

So, if we're going to put things in memory, we need to know where we put them if we are going to do something to those things later. Every single location in memory has an address. You get to a particular address by storing a number in a CPU register. So if you want to look at memory address 100, you'd stick 100 in one of the CPU registers, and voila, that's pointing at memory address 100 (or is pointing at nothing and is just storing 100 for you - but we're not going to get into how CPU registers work here).

CPU registers can hold numbers from 0 up to however big they are. That could mean numbers from 0 to 255 (an 8 bit register), 0 to 65535 (16 bits) and on up. But notice it's always "0 to something". So if having 100 means you are pointing at memory location 100, what does having 0 in there mean?

Well, it could have meant "you made a big mistake, the first location is one". Actually, because of the kinds of mistakes programmers sometimes make, it might have been useful and interesting if CPU's had been designed that way. But they weren't, and actually I've simplified things here, so of course now we need to get a little more complicated.

When CPU's want to address memory, they actually usually do so by using two registers: a base and an offset. So you store 100 in one register, and 2 in another, and that means you are pointing at memory address 102 (100 + 2). What's the point of this? Why use two when one would do? Well, some of that is due to some legacy issues of early CPU's having some hardware limitations: it's called segment addressing, and it will make you unhappy, so we won't get into that here. But it also helps program design in general.

(That's not really how segmented addressing works but the rest will be easier for you to understand if we pretend it is.)

Arrays

If 100 is in our base register, and 0 is in the offset, there we are pointing exactly at address 100. Add 0 to 100 and you get 100. Prgrammers use this to build higher level s

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.