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
  • 1
  • 10
  • 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 06:56
  • 6
    @devnull, but so does "first element of array" :-) – Péter Török Sep 20 '11 at 06:58
  • @PéterTörök i was thinking from the human point of view as greengit pointed out. – devnull Sep 20 '11 at 07: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!" – Christopher Mahan 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 08 '12 at 07:06

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,475
  • 10
  • 47
  • 84
  • I am assuming of course your compiler/runtime error message would use a consistent line numbering method as your text editors and other text manipulation tools, which makes the readability point moot. – wim Sep 20 '11 at 07:03
  • 16
    +1 line numbers are for humans not computers. – devnull Sep 20 '11 at 07:08
  • 6
    don't upvote my comment, it's quoted from the answer :) – devnull Sep 20 '11 at 07: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... – Marjan Venema Sep 20 '11 at 07:41
  • 10
    "All humans are not programmers" is false - I am a human and a programmer. :) (I think you meant "not all humans are programmers".) – Dave Sherohman 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
  • 1
    @Rook: Don't worry. I know better than to make that joke anywhere other than in a room full of programmers. – Dave Sherohman Sep 21 '11 at 09:22
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.

Péter Török
  • 46,427
  • 16
  • 160
  • 185
  • 4
    Much more than decades: The Bible numbering starts at 1. – mouviciel Sep 20 '11 at 07: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. – Marjan Venema Sep 20 '11 at 07: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 08:55
  • 2
    [0-indexed numbering makes more sense](http://www.henrycipolla.com/blog/2011/03/0-indexed-numbering-makes-more-sense-in-programming-than-1-indexing-dijkstra-agrees/). Especially the point Dijkstra makes, makes sense. – Lieven Keersmaekers 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 :-) – Péter Török Sep 20 '11 at 10:58
  • @Michael: Though the length is indeed immediately to the left of the data of the string, it takes up more space than a byte. Reading String[0] will only give you a correct value if the length is less than 256. Which was indeed the case for ShortStrings (max length 255). With variable length strings the length is stored in a longint. And to the left of that you will find the reference count, and since D2009 the element size and codepage/encoding. – Marjan Venema Sep 20 '11 at 11:03
  • @Péter - I'm not questioning line numbering starting from 1 but with *it is only prevalent in C and its successors.* it was my impression you think of it to be a bad idea in programming too. – Lieven Keersmaekers Sep 20 '11 at 11:30
  • @Lieven, no, I don't think it is a bad idea per se. In fact, having worked in languages using 0-based indexing almost my whole professional career, I appreciate its inner logic and probably would have difficulties working with a 1-based language. Still that doesn't mean everyone else (incl. ordinary users) should learn and use this idiom and only this. Logic is one thing, zealotry is another :-) – Péter Török Sep 20 '11 at 12:12
  • @MarjanVenema, I did say traditional Pascal. I was thinking of environments like Turbo Pascal 7 and friends, not modern incarnations of Delphi. – user Sep 20 '11 at 14:34
  • An article (there were two, but I can't find the other one) which also argues http://alarmingdevelopment.org/?p=470 – Rook Sep 20 '11 at 16:16
  • @Michael: Though I have been in programming for some time now (26+ years), my memory doesn't go that far back for Pascal :-) I am not sure when long strings (>255 chars) were introduced into the Pascal language. They were certainly available in Delphi 3 (1997/8) as I don't remember ever programming Delphi without them and I started with D3. Anyhow, as soon as long strings were available, relying on s[0] would have been suspect unless you were just checking for non-zero and even then Length(s) would have done the job just as well. – Marjan Venema Sep 20 '11 at 17:30
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
  • 8,692
  • 1
  • 20
  • 32
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,261
  • 24
  • 28
  • 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 07:20
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,406
  • 14
  • 106
  • 154
  • 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 :-) – Péter Török Sep 20 '11 at 07: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 07:44
  • 1
    Wow, at long last we found other logical beings in the Universe! Hi buddy, which planet you are from? ;-) – Péter Török Sep 20 '11 at 07:51
  • 1
    I thought that all European countries count stories from 0 (ground floor), and continue up with 1st floor, 2nd floor, etc.. – Mark Northrop 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
  • @Rook, it's not that people refer to it as Floor 0 in speech (although it does happen), usually it is called Ground Floor in Hungarian. However, in elevators, you often see it marked by 0, not by a letter. – Péter Török Sep 20 '11 at 21:35
  • @PéterTörök - Oh, I see. Interesting, I never saw that. How do they refer to those half-floors between the ground floor and the first floor? (usually maintenance rooms on those, less flats) – Rook Sep 20 '11 at 23:17
  • Norway's ground floor is called "første-etasje" (with or without a dash). Meaning "1st floor". Second floor is andreetasje, and so forth. Underground floors are "første underetasje" (first floor below), and so forth. Zero is not even a thing. Spain uses "planta baja" (ground floor), and "primera planta" for the one above. I would say "primer piso inferior" for the first underground floor. (First inferior floor). – mazunki Nov 20 '19 at 20:54