in software

Why C Arrays Start at Zero: I Don’t Know.

Why do C arrays start at element zero? It’s probably not why you think.

Mike Hoye did a detailed investigation here: http://exple.tive.org/blarg/2013/10/22/citation-needed/

The TL;DR is that Hoye says most programmers think C has zero-indexed arrays because the creator of C chose zero for the run-time efficiency of pointer and array arithmetic. Hoye says this is wrong; he claims that C uses zero-indexed arrays because its predecessor BCPL used them, and that BCPL chose zero-indexed arrays for compile-time efficiency. (I’m not convinced by his technical arguments, by the way, but that’s beside the point I’d like to make.)

The history of zero-indexing is interesting if you enjoy reading about the history of programming languages. Otherwise it’s hardly notable.

Except.

Except you probably thought you knew why C arrays start at element zero. “Well, it’s because pointer and array math are efficient when starting at index zero.” If asked, you’d probably say it confidently, definitively, as a Fact with a Capital F. You might even be correct, but you’re only guessing.

Hoye goes on to say that programmers who think they know something as Fact without actually knowing it are living in a “mythology.” They’re fooling themselves and others.

The honest answer to “why zero?” should probably be “I don’t know.” Perhaps followed by, “I think it makes some pointer and array operations more efficient at the machine level, but I don’t know why Dennis Ritchie chose it when he created C.”

We don’t use the phrase “I don’t know” often enough. Maybe we’re afraid of being thought of as dumb or ignorant or more junior than we want to appear, or maybe we’re afraid of being found out as impostors.

Recognizing and admitting your ignorance is one of the fastest ways to learn. Recognize and admit when you don’t know something, at least to yourself.

Why did it take me 20 years of being a software engineer to realize how important this is?

I don’t know.


 

I wrote this piece for Embedded.fm (http://embedded.fm/blog/2016/2/9/why-c-arrays-start-at-zero) and am cross-posting it here.

Write a Comment

Comment