Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
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
3  
@devnull, but so does "first element of array" :-) –  Péter Török 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
4  
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. –  systemovich Feb 8 '12 at 7:06
add comment

marked as duplicate by ChrisF Sep 20 '11 at 12:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
And of course, most normal humans in their daily lives still count from 1. –  Rook Sep 20 '11 at 16:11
add comment

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

share|improve this answer
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
add comment
  • 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.

share|improve this answer
15  
+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
6  
@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 7:41
4  
"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
show 2 more comments

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.

share|improve this answer
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. –  Marjan Venema Sep 20 '11 at 7:44
    
@Marjan, thanks for the clarification, fixed. –  Péter Török Sep 20 '11 at 7:46
    
@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.) –  Michael Kjörling Sep 20 '11 at 8:55
1  
0-indexed numbering makes more sense. Especially the point Dijkstra makes, makes sense. –  Lieven Keersmaekers Sep 20 '11 at 10:37
show 7 more comments

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

share|improve this answer
    
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 7:39
    
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? ;-) –  Péter Török Sep 20 '11 at 7:51
    
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
show 2 more comments

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