18

Okay this sounds like a question a six year old would ask, (and I hope there is an empirical answer) so here's my inner child: In programming numbers start from from zero but why do line numbers in text editors (for programming) also start from one?

2
  • 6
    Zero index signifies memory address (first place of the memory block) whereas line 1 is plain number 1. Human, including programmers, count thing starting from 1. Non-programmers usually confusing between things and places.
    – 9dan
    Jan 16, 2013 at 15:27
  • 3
    Not all programming languages start array indices at 0. I suppose most modern languages do, but I think program line numbers beginning at 1 predate array indices starting at 0.
    – obelia
    Jan 19, 2013 at 4:51

7 Answers 7

22

Line numbers need to be countable. It is more intuitive to start counting at 1 instead of 0, because 0 (say: zero) means none, not one.

Looking at a coffee mug on your table, you would not answer the question of how many mugs there are by saying: "Zero" - because that would imply no mug at all is on your table. Thus with line numbers, and almost every other countable thing there is, you'd start with 1 for logic reasons.

Your real questions should be: Why on earth do array indices and the like start from 0 in programming! :)

8
  • 3
    You might find the following useful: en.wikipedia.org/wiki/Zero-based_numbering
    – CJF
    Jan 16, 2013 at 13:01
  • 4
    I think this answer is best summarized as Because only programmers start counting at 0, and not only programmers use text editors.
    – SpellingD
    Jan 16, 2013 at 15:16
  • 8
    Because only programmers start indexing at 0 :)
    – kontur
    Jan 16, 2013 at 15:18
  • s/indixes/indices/
    – steveax
    Feb 22, 2013 at 20:16
  • Do you ever count lines like this, though? To me, line numbers are a clear-cut example of indexing, thus zero-basing would have made things easier for users.
    – mudri
    Oct 9, 2014 at 8:10
17

Kontur is correct. But I would also like the add that Arrays in programming start at 0 for a very specific reason. This is not because the number in an array is supposed to 'count' the amount of elements, but instead it is considered an offset value, and thus array[0] merely means that the specific entry is 0 memory positions away from the start of the array in memory and array[1] is 1 position along. They are not used for counting purposes but as an offset meaning it is perfectly logical that programming lists start at 0.

2
  • 3
    I would agree that this is the historical reason array indexes started at zero (when array indexes were mapped to memory addresses), but would argue that the reason it is still this way has more to do with the mathematical convenience of dealing with 0-based indexes than anything to do with memory offsets.
    – devios1
    Jan 17, 2013 at 19:25
  • 2
    Counting arrays from 0 is C (where you have pointer arithmetics); Algol68 etc. starts at 1. See here. Jan 17, 2013 at 19:37
6

Early languages like FORTRAN had the first element of an array starting with 1, and it was weird when C came along to use 0. That's only natural to you youngsters. FORTRAN started with 1 because it was natural to number things in a list starting with 1. C started with 0 because it was a language deliberately written to be close to the hardware, whereas FORTRAN was abstracting the mathematical concepts with no idea as to how the computer would implement it. Of course we had to be able to know in some cases how the code was implemented in machine language, but in general C is much closer to the hardware.

1
  • Good point about abstraction also! There is a good reason why starting at 0 makes sense under the angle of technical logic, but that has little to do with common sense logic.
    – kontur
    Jan 31, 2013 at 11:30
1

It is purely historical / traditional / cultural. In many ways one can argue that line numbers should start at zero. But text editors have always labeled the first line as line one, and any editor that labeled it as zero would be outcast.

Generally, the difference between starting at 1 and starting at zero is whether you are "counting" or "measuring". If you measure a highway, it is clear that the point where the highway starts is "zero". But what do you call that first mile? Is it "mile 0" or is it the "first mile"? Both are acceptable depending upon whether you are measuring the distance, or counting the miles. This is very much like centuries: 1950 is the middle of the "20th century". The distinction is the 1950 is a measure of the time since the beginning (except there was no year zero, another historical anomaly) and the other is a counting of the centuries.

In an array, we are "measuring" to the position, and this is preferred not because it is closer to the hardware, but because the math works out easier. The first element in an array is always 0 units from the beginning of the array. This becomes quite apparent when you have an array of arrays. Imagine an array of decades, each element of which is an array of years. You would want the years to run 0 thru 9 to represent a decade like 1950 thru 1959. While it could be implemented either way (the first year of the decade is 1950, which is zero years from the start of the decade). The math is easier if the you can simply subtract to find the offset = (year - decade_start).

All that being said, the logic is that the first line of a text file really should be called "line 0" because you are measuring the position in the text file. But it is not. I don't believe there is a logical reason for that, but simply that it has been traditionally labeled with a "1". Perhaps in the same tradition that the first year in the Gregorian calendar is named "1" as well.

0

Starting from zero sounds a bit too technical to a six year old inside me :)

It probably gives an impression that

  • first line will not be counted,

  • or the code lines have not started from here,

  • or it is only for the comments.

0

We (programmers) don't actually count from zero, I think that's a misunderstanding. The first element in an array is at index (or offset) zero (but it's still the first element, i.e. element number one).

So there's no reason to count lines from zero, because no one counts from zero.

0

In fact if your editor shows relative line numbers, you'll see that the current line is number "0".

You can check by yourself in any recent version of vim (> 7.3) by using the command:

:set relativenumber

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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