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.

  • 5
    "first line of code" refers to 1 rather than 0, so that's the most likely cause to have 1 based line numbering.
    – devnull
    Sep 20 '11 at 6:56
  • 6
    @devnull, but so does "first element of array" :-) Sep 20 '11 at 6:58
  • @PéterTörök i was thinking from the human point of view as greengit pointed out.
    – devnull
    Sep 20 '11 at 7:11
  • 5
    It's silly outside of programming. Imagine this: A geek programmer girl tells her dad: "I swear I had zero boys in my room last night!" Sep 20 '11 at 15:59
  • This is not an exact duplicate. The "Possible Duplicate" refers to Visual Studio specifically. This question is general. The title of the Visual Studio question should perhaps be changed to make it general.
    – Geoffrey
    Feb 8 '12 at 7:06

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.

  • 1
    And of course, most normal humans in their daily lives still count from 1.
    – Rook
    Sep 20 '11 at 16:11
  • 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.

  • 16
    +1 line numbers are for humans not computers.
    – devnull
    Sep 20 '11 at 7:08
  • 6
    don't upvote my comment, it's quoted from the answer :)
    – devnull
    Sep 20 '11 at 7:20
  • 7
    @devnull: if the comment contains "+1" and I upvote that comment, I already upvoted the answer. I upvote the comment as well because I agree with the reason for upvoting the answer... Sep 20 '11 at 7:41
  • 9
    "All humans are not programmers" is false - I am a human and a programmer. :) (I think you meant "not all humans are programmers".) Sep 20 '11 at 11:04
  • 1
    @Dave - Don't do that. This is one of the reasons why most humans think programmers are weird. They take something which most understand and start making a "problem" out of it.
    – Rook
    Sep 20 '11 at 16:13

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.

  • 4
    Much more than decades: The Bible numbering starts at 1.
    – mouviciel
    Sep 20 '11 at 7:39
  • 1
    Indexing is zero-based in Pascal for almost everything (lists, dynamic arrays etc.). It can be anything based for static arrays: for example array[13..15] of Integer. It is only 1 based for indexing characters in strings. Sep 20 '11 at 7:44
  • @MarjanVenema, isn't that because in traditional Pascal, the first byte of the string indicated the length? (Also having the effect of limiting normal strings to a maximum length of 255 bytes.)
    – user
    Sep 20 '11 at 8:55
  • 2
    0-indexed numbering makes more sense. Especially the point Dijkstra makes, makes sense. Sep 20 '11 at 10:37
  • @Lieven, again, that may make sense for programmers and mathematicians, but try to explain to a layperson why books should start with page 0 :-) Sep 20 '11 at 10:58

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

  • 2
    Actually, you could number lines by 1,2,3.. and line 0 was legal too. Numbering by 10 was done for maintainablity - it's easier to insert "gosub 1100" at line 15 between 10 and 20 in your program of 50 lines numbered 10,20,30...-500, than to renum all lines 2-50 in a program with line numbers 1,2,3...50 to make room for a new line 2.
    – SF.
    Sep 20 '11 at 7:20

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)).

  • 2
    One exception is counting storeys of buildings in Hungary. In most countries I know, the ground floor is Level 1, first floor is Level 2 etc. However in Hungary, ground floor is Level 0, so first floor is conveniently Level 1 etc. But they say we are from another planet anyway :-) Sep 20 '11 at 7:39
  • 2
    We do the same in the Netherlands - that's because the ground floor has a different name ('begane grond') than the rest of them ('verdieping'); but also, it makes sense because storeys go both ways - zero is at ground level, negative numbers are below, positive above.
    – tdammers
    Sep 20 '11 at 7:44
  • 1
    Wow, at long last we found other logical beings in the Universe! Hi buddy, which planet you are from? ;-) Sep 20 '11 at 7:51
  • 1
    I thought that all European countries count stories from 0 (ground floor), and continue up with 1st floor, 2nd floor, etc..
    – mtnorthrop
    Sep 20 '11 at 12:10
  • @PéterTörök - In Croatia, the first floor is also Floor 1/Level 1. The ground floor is simply "the ground floor". Nobody referrs to it as Floor 0/Level 0 (?!).
    – Rook
    Sep 20 '11 at 16:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.