6

Possible Duplicate:
Why isn't the line count in Visual Studio zero-based?

Why isn't the top line in a source code file labelled line number 0?

i.e. in a source file which was 10 lines long, I would expect line numbers would be 0-9, but they're not! Now grep, awk, sed etc all seem to number from 1, so I'm assuming it's an established convention for a good reason.

It occurred to me recently that, for almost every other structure a programmer has to deal with in their day to day work, the convention is to count from 0 - and a source file is as much like a list of lines as a line is like a list of chars (afaik list of chars is invariably 0-indexed). I had to actually open up my text editor and sanity check that the top line of a file was labelled line number 1!

I was just wondering if there is a good reason for this or if it's just an unfortunate historical convention.

wim
  • 198

5 Answers5

46
  • 0-based indexing is for computers not humans
  • line numbers are for humans not computers
  • all humans are not programmers

1-based line numbers ease readability a tiny bit -- otherwise every time you saw an error message with a line number, you'd have to remember to decrement it by one to get to the actual line that caused the error -- and that impedes readability.

Also line 1 reads better than line 0 -- because others (people who may be non-technical) may have to read your code.

treecoder
  • 9,495
10

for almost every other structure a programmer has to deal with in their day to day work, the convention is to count from 0

This is language specific, and AFAIK it is only prevalent in C and its successors. AFAIR indexing is 1-based at least in the Pascal / Modula and BASIC family, and possibly in functional languages too (which don't use indexing much anyway, rather iterators or foreach etc.).

In C, it made array/pointer arithmetic convenient. However, I don't think anyone can call it intuitive - I believe it took some time for all of us to get used to it. It is, as @greengit pointed out, a hardware-level concept, or rather an implementation detail. Only (C et al) programmers need to understand and use 0-based indexing, but try to explain it to a layperson :-)

When dealing with lines of file, you rarely do pointer arithmetic - a file of text lines is simply a higher level of abstraction. And more so, as text files are used by a lot of people who aren't C programmers, just "simple" users. Making their life more complicated by forcing them to learn a low level implementation detail would be totally counterproductive.

And one more important thing: text files (and before them, punch cards) have been around for decades before C came into existence, so I believe by the time C was created, counting their lines/rows from 1 was already a well established convention.

7

Punch cards started at line 1: enter image description here

The tradition stuck even when we stopped using punch cards. And of course, early languages like FORTRAN, COBOL and ALGOL all used 1-based indexing.

MSalters
  • 9,038
4

I have programmed in Basic. There the numbers would typically be 10, 20, 30. You could add extra lines like 15 and then use "renum" to make 10, 20, 30, 40. And these numbers were important as you used "goto 15".

So lines haven't always been numbered as 1,2,3

Carra
  • 4,281
1

The "natural" thing to do is 1-based counting, like we do with line numbers. Most real-life entities that we number serially start at 1 - bus lines, pop chart positions, highways, flights, - none of these ever have number 0.

0-based is the exception, not the rule, but there are a few very good technical reasons why we use them, the most important being that it makes pointer arithmetic work naturally, especially in low-level languages like C (a[3] means 'take the address a and step 3 cells forward', i.e., *(a + 3)).

tdammers
  • 52,936