Search code examples
indexingconventions

Why is there a convention of 1-based line numbers but 0-based char numbers?


According to TkDocs:

The "1.0" here represents where to insert the text, and can be read as "line 1, character 0". This refers to the first character of the first line; for historical conventions related to how programmers normally refer to lines and characters, line numbers are 1-based, and character numbers are 0-based.

I hadn't heard of this convention before, and I can't find anything relevant on Google. Can anyone explain this to me please?


Solution

  • I think you're referring to Tk's text widget. The man page says:

    Lines are numbered from 1 for consistency with other UNIX programs that use this numbering scheme.

    Although, I'm not sure which Unix tools it's talking about.

    Update: As mentioned in the comments, it looks like a lot of unix text manipulation tool starts line numbering at 1. And tcl/tk having a unix origin, it makes sense to be as compatible as possible with the underlying OS environment.